先说下我要实现的功能:两个和屏幕一样大小的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布局
现在我实现了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布局
解决方案 »
- android 【地图开发】
- 关于ADT的安装问题
- 往sdcard中已存在的xml文件追加数据该怎么做呢?
- 我想实现一个功能,大家来给个意见。
- 地图在模拟器上能正常显示,在手机上显示不了
- 请教androidMainfest.xml相关问题???
- 如何实现将Android源码中的Browser在退出时关闭网络连接?
- android 页面布局问题
- 怎么将两个字的string中间加个空格
- Android TextView显示泰语高度变化显示不全的问题
- android页面布局 如何让中间的listView填充剩余部分
- This tag and its children can be replaced by one <TextView/> and a compound draw
自定义ViewGroup时,设置自身及其子元素大小必须在其onMeasure方法里调用measure设置,在onLayout里调用measure无效!希望对遇到同样问题的朋友不要在这个问题上浪费时间。