自定义ViewGroup中不能正确显示 其它的viewGroup
问题描述:
小弟写了一个MyViewGroup(extends ViewGroup),在MyViewGroup中可以正确显示child view都是widget类型的(例如Button,ImageButton),但不能正确显示其它的ViewGroup比如系统的RelativeLayout。我在xml中RelativeLayout里面只定义了一个ImageButton ,这个ImageButton能够显示出来但显示的位置与xml中定义的不同。
测试类 testViewClass.java :
MyViewGroup.java
相对布局的xml文件 mute_box.xml:
小弟写了一个MyViewGroup(extends ViewGroup),在MyViewGroup中可以正确显示child view都是widget类型的(例如Button,ImageButton),但不能正确显示其它的ViewGroup比如系统的RelativeLayout。我在xml中RelativeLayout里面只定义了一个ImageButton ,这个ImageButton能够显示出来但显示的位置与xml中定义的不同。
测试类 testViewClass.java :
package com.yht.testViewClass;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
public class testViewClass extends Activity {
/** Called when the activity is first created. */
MyViewGroup mvg;
LayoutInflater layoutInflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mvg = new MyViewGroup(this);
this.setContentView(mvg);
layoutInflater = this.getLayoutInflater();
RelativeLayout view = (RelativeLayout) layoutInflater.inflate(R.layout.mute_box, null);
mvg.addView(view);
}
}
MyViewGroup.java
package com.yht.testViewClass;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
public class MyViewGroup extends ViewGroup {
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyViewGroup(Context context) {
super(context);
}
@Override
protected void onLayout(boolean arg0, int l, int t, int r, int b) {
int count = this.getChildCount();
for(int i = 0;i < count;i++){
View child = this.getChildAt(i);
child.setVisibility(View.VISIBLE);
child.measure(r-l, b-t);
int x = l;
int y = t;
//心中疑惑:我认为我只要在这里调用child的layout(int,int,int,int)方法给出它的位置和宽高就可以了
//如果child本身是一个ViewGroup的话它是否应该自己去管理它本身内部的child的位置和宽度呢???
child.layout(x,y,x + getWidth(),y + getHeight());
}
}
}
相对布局的xml文件 mute_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/list_background">
<!--
Button 相对于Parent 垂直靠右显示
-->
<Button
android:id="@+id/image_button_2"
android:text="TEST BUTTON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
...全文
相关推荐
Android自定义ViewGroup之WaterfallLayout(二)
上一篇我们学习了自定义ViewGroup的基本步骤,并做了一个CustomGridLayout的实例,这篇我们继续来说说自定义ViewGroup。 Android中当有大量照片需要展示的时候,我们可以用GridView作为照片墙,但是GridView太整齐了,有时候不规则也是一种美,瀑布流模型就是这样一个不规则的展示墙,接下来我们尝试用自定义ViewGroup来实现瀑布流。 实现瀑布流的方式也有很多,下面我们一一道来: 一、继承ViewGroup 其实这种实现方式我们只需要在上篇博客的基础上稍作修改即可,主要修改这几个地方: •LayoutParams 因为瀑布流中每张图片宽度设为相
Android-一个类似思维导图软件的ViewGroup
一个类似思维导图软件的ViewGroup
自定义控件量测主过程
实现Android量测主过程Demo,从单个View到ViewGroup的量测实现。
子view突出部分不能响应点击
android:clipChildren="false"下突出view部分无法响应点击
Android自定义View实现开关按钮
android 自定义view知识非常广泛,难以让人掌握。但是也是andoroid进阶学习的必经之路。下面通过本文给大家介绍Android自定义View实现开关按钮的知识,非常不错,感兴趣的朋友一起看看吧


