先说下我要实现的功能:两个和屏幕一样大小的view竖直方向上排列,默认只显示其实一个,拖拽view进行滑屏切换。
现在我实现了view之间的切换,但问题是,view加入自定义viewgroup后,布局变了。单独显示该view又是正常的。
为了方便大家,我另外写了个简单的demo,代码很短,下面贴出来,拜托大家帮忙看看:
入口Activity:package com.example.viewgrouptest;import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;public class MainActivity extends Activity {

private MyTestViewGroup myViewGroup;
private LayoutInflater layoutInflater;
private View firstView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//单独显示一个view,表现正常,TextView全屏
//setContentView(R.layout.first_view);

//显示ViewGroup,TextView尺寸变了
setConteViewGroup();

}

public void setConteViewGroup() {
setContentView(R.layout.viewgroup);
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
firstView = layoutInflater.inflate(R.layout.first_view, null);
myViewGroup = (MyTestViewGroup)findViewById(R.id.myViewGroup);
myViewGroup.addView(firstView); 
}
}
自定义ViewGroup:package com.example.viewgrouptest;import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;public class MyTestViewGroup extends ViewGroup {

private View firstView; public MyTestViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
} protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(changed) {
firstView = getChildAt(0);
firstView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
firstView.layout(0, 0, getWidth(), getHeight());
}
}}
自定义ViewGroup的xml布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/viewgroupLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
android:orientation="vertical"> <com.example.viewgrouptest.MyTestViewGroup
    android:id="@+id/myViewGroup"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</com.example.viewgrouptest.MyTestViewGroup>
</LinearLayout>first_view的布局xml:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/secondLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF0000" >
    
    <TextView
        android:id="@+id/firstView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        android:textSize="20sp"
        android:text="尺寸问题" /></LinearLayout>最后是AndroidManifest文件,没什么特别的:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.viewgrouptest"
    android:versionCode="1"
    android:versionName="1.0" >    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.viewgrouptest.MainActivity"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application></manifest>
viewgroupAndroid布局