码迷,mamicode.com
首页 > 移动开发 > 详细

android自定义布局的使用!

时间:2014-10-24 16:38:21      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   io   os   ar   使用   

继承viewGroup;

自定义控件的左边距;右边距;上边距,下边距;


java 代码;

package com.example.customview1406_04myviwegroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
* 自定义的一种布局,指定每个子控件如何显示
*
* @author gsd1403
*
*/
public class MyViewGroup extends ViewGroup {

public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

/**
* 测量控件的大小
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 测量子控件的大小
// 子控件的个数
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++) {
// 得到子控件
View view = this.getChildAt(i);
view.measure(widthMeasureSpec, heightMeasureSpec);
}
}

public void setShowView(int index) {
if (index > this.getChildCount()) {
return;
}
if (index == 0) {
View view = this.getChildAt(0);
view.setVisibility(View.VISIBLE);
this.getChildAt(1).setVisibility(View.GONE);
} else if (index == 1) {
this.getChildAt(1).setVisibility(View.VISIBLE);
this.getChildAt(0).setVisibility(View.GONE);
}
}

/**
* 指定子控件如何显示 类似于onDraw
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 得到第一个控件 text.xml
// View view1=this.getChildAt(0);
// view1.layout(0, 0, view1.getMeasuredWidth(),
// view1.getMeasuredHeight());
//
// View view2=this.getChildAt(1);
// view2.layout(60, 200, view2.getMeasuredWidth(),
// view2.getMeasuredHeight());
int childCount = this.getChildCount();
int left = 0;
for (int i = 0; i < childCount; i++) {
View view = this.getChildAt(i);
if (view.getVisibility() != View.GONE) {
view.layout(left, 0, left + view.getMeasuredWidth(),
view.getMeasuredHeight());
left = left + view.getMeasuredWidth();
}
}

}

}

MainActivity 代码;


package com.example.customview1406_04myviwegroup;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
MyViewGroup myViewGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewGroup=(MyViewGroup) findViewById(R.id.myViewGroup);
Button btnAudio=(Button) findViewById(R.id.button_audio);
btnAudio.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myViewGroup.setShowView(1);
}
});

Button btnText=(Button) findViewById(R.id.button_text);
btnText.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myViewGroup.setShowView(0);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


布局文件;代码;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.example.customview1406_04myviwegroup.MyViewGroup
        android:id="@+id/myViewGroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
       <include  layout="@layout/text"/>
        <include layout="@layout/audio"/>
    </com.example.customview1406_04myviwegroup.MyViewGroup>

</RelativeLayout>


bubuko.com,布布扣


点击audio按钮之后的效果如下图;

bubuko.com,布布扣


android自定义布局的使用!

标签:android   style   blog   http   color   io   os   ar   使用   

原文地址:http://blog.csdn.net/android_drawing/article/details/40427741

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!