自定义Tabhost里的TabWidget布局
2012-02-21 11:16
399 查看
首先要理解Tabwidget.
TabWidget 理解:
1. TabWidget 为 horizontal 的 LinearLayout
2. 且 其包含的标签又是一个RelativeLayout
3. 每个标签RelativeLayout 里面包含2个View: TextView ImageView
有了这个理解,可以估计得到
view
plaincopy
to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView />
<TextView />
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView />
<TextView />
</RelativeLayout>
...多个RelativeLayout
</LinearLayout>
有了这个布局,就可以自定义自己的布局了,以图片在左,文字在右为例。
view
plaincopy
to clipboardprint?
private void addToTabHost(String name, int image, int number) {
// TODO Auto-generated method stub
LinearLayout ll = (LinearLayout) mTabHost.getChildAt(0);
TabWidget tw = (TabWidget) ll.getChildAt(0);
RelativeLayout rl = (RelativeLayout) tw.getChildAt(number);
rl.addView(composeLayout(name, image), new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
}
private View composeLayout(String name, int image) {
LinearLayout layout = new LinearLayout(this);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.HORIZONTAL);
ImageView iv = new ImageView(this);
iv.setImageResource(image);
layout.addView(iv, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT));
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setSingleLine(true);
tv.setPadding(10, 0, 0, 0);
tv.setTextSize(18);
tv.setText(name);
layout.addView(tv, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT));
return layout;
}
TabWidget 理解:
1. TabWidget 为 horizontal 的 LinearLayout
2. 且 其包含的标签又是一个RelativeLayout
3. 每个标签RelativeLayout 里面包含2个View: TextView ImageView
有了这个理解,可以估计得到
view
plaincopy
to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView />
<TextView />
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView />
<TextView />
</RelativeLayout>
...多个RelativeLayout
</LinearLayout>
有了这个布局,就可以自定义自己的布局了,以图片在左,文字在右为例。
view
plaincopy
to clipboardprint?
private void addToTabHost(String name, int image, int number) {
// TODO Auto-generated method stub
LinearLayout ll = (LinearLayout) mTabHost.getChildAt(0);
TabWidget tw = (TabWidget) ll.getChildAt(0);
RelativeLayout rl = (RelativeLayout) tw.getChildAt(number);
rl.addView(composeLayout(name, image), new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
}
private View composeLayout(String name, int image) {
LinearLayout layout = new LinearLayout(this);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.HORIZONTAL);
ImageView iv = new ImageView(this);
iv.setImageResource(image);
layout.addView(iv, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT));
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setSingleLine(true);
tv.setPadding(10, 0, 0, 0);
tv.setTextSize(18);
tv.setText(name);
layout.addView(tv, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT));
return layout;
}
相关文章推荐
- 自定义TabHost,TabWidget样式 .
- 自定义TabHost,TabWidget样式 .
- 自定义TabHost,TabWidget样式
- 自定义TabHost,TabWidget样式 .
- 自定义TabHost,TabWidget样式 .
- android学习笔记《三》:TabHost中自定义TabWidget
- Android程序开发之自定义设置TabHost,TabWidget样式
- Android中的切换卡TabWidget布局简介
- ListView 自定义item布局 android.widget.LinearLayout$LayoutParams
- 5 Your TabHost must have a TabWidget whose id attribute is ‘android.R.id.tabcontent’
- 安卓控件使用系列29:TabHost卡片的使用方法2不继承TabActivity、TabHost控件、一个布局文件
- 选项卡TabHost、TabWidget和FrameLayout
- Button 的文字怎么竖向显示 自定义竖型TabWidget ListView的初始化滚动条定位到最后一行
- TabWidget/FragmentTabHost去掉分割线:
- 安卓控件使用系列30:TabHost卡片的使用方法3不继承TabActivity、TabHost控件、多个布局文件
- 深入浅出android/ophone UI实现水平布局的Tab控件 Tabwidget在左边/右边
- Android 实现分页(使用TabWidget/TabHost)
- TabLayout自定义布局(标题实现三个颜色)
- android设置Tabhost中的TabWidget在Framelayout的下方
- tabhost(tabweight自定义)