android自定义布局的使用!
来源:互联网 发布:s52射钉弹淘宝叫什么 编辑:程序博客网 时间:2024/04/29 10:28
继承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"
>
- android自定义布局的使用!
- android自定义布局的使用!
- Android Tablayout 自定义Tab布局的使用
- android的XML布局文件上使用自定义View类
- android一个BaseAdapter的使用(LayoutInflater加载自定义布局)
- android使用5.0的toolbar,自定义布局参考地址
- android中RecyclerView使用自定义的列表布局
- Android学习笔记--Notification的使用-- 自定义布局
- Android自定义dialog的布局
- AlertDialog使用自定义的布局
- Android给dialog使用自定义布局时,布局内容无法完全显示的问题
- android 在布局中使用自定义属性
- Android使用自定义布局制作进度圆
- Android界面美化 -- 使用自定义Toast布局替代原生布局
- 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用
- android自定义的一个时间秒表的布局
- 自定义Android标题栏修改TitleBar的布局
- android notification自定义布局的应用
- hadoop2.52 HA方案安装与hbase1.0.0 HA方案安装 详细步骤
- 分治策略——最大子数组问题
- HTML5中声明了UTF-8还是中文乱码解决
- SpringMVC初识
- 展讯7731平台驱动调试总结(1)---- 系统编译环境安装
- android自定义布局的使用!
- 9中排序算法
- C之去除字符串之相邻字符
- Hadoop 实战 - 访问HDFS出错
- 数据结构之数组(对数组的所有操作!)
- LeetCode刷题记录1-ZigZag Conversion
- Android中Context详解 ---- 你所不知道的Context
- 第十二周项目三日期时间类
- php 打包图片