在Android的定制fragmentTabhost [英] Customising fragmentTabhost in android
本文介绍了在Android的定制fragmentTabhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在做什么的:
- 我有一个片段tabhost
- 作为部份效果如图所示我想删除蓝色的选择
编程如果可能的话还有其他明智
问题:
- 可以这样进行? ......如果是这样如何?
- 和TAB的的onclick我想改变背景
MainActivity.java
公共类MainActivity扩展SherlockFragmentActivity {
//声明变量
私人FragmentTabHost mTabHost; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
//设置从main_fragment.xml视图
的setContentView(R.layout.main_fragment); //在main_fragment.xml找到android.R.id.tabhost
mTabHost =(FragmentTabHost)findViewById(android.R.id.tabhost); //创建main_fragment.xml标签
mTabHost.setup(这一点,getSupportFragmentManager(),R.id.tabcontent); //在res文件夹的自定义图像创建TAB1
mTabHost.addTab(mTabHost.newTabSpec(等级)。setIndicator(等级),
FragmentTab1.class,NULL); //创建TAB2
mTabHost.addTab(mTabHost.newTabSpec(价格)。setIndicator(价格),
FragmentTab2.class,NULL); //创建TAB3
mTabHost.addTab(mTabHost.newTabSpec(距离)。setIndicator(距离),
FragmentTab3.class,NULL);
}
}
解决方案
//删除您需要设置TabWidget的属性选项卡下方的条形
的android:tabStripEnabled =假
//创建一个布局tab_indicator.xml
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / relIndicator
机器人:layout_width =@扪/ tab_width
机器人:layout_height =@扪/ tab_height
机器人:重力=center_vertical
机器人:可点击=真正的> < RelativeLayout的
机器人:ID =@ + ID / relTabIcon
机器人:layout_width =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:可点击=真
机器人:可聚焦=真
机器人:focusableInTouchMode =真
机器人:layout_height =30dip> < ImageView的
机器人:ID =@ + ID / inner_icon
机器人:layout_width =30dip
机器人:layout_height =30dip
机器人:layout_centerHorizontal =真
机器人:layout_centerInParent =真
机器人:可点击=真
机器人:contentDescription =@字符串/ APP_NAME
机器人:知名度=看得见/>
< / RelativeLayout的> <的TextView
机器人:ID =@ + ID /标题
风格=@风格/ txtStyleWhite
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =25dip
机器人:layout_below =@ + ID / relTabIcon
机器人:layout_centerHorizontal =真
机器人:paddingRight =2DP
机器人:paddingLeft =2DP
机器人:比重=中心
机器人:文字=个人文本
机器人:TEXTSIZE =@扪/ tab_font
机器人:知名度=看得见/>
< / RelativeLayout的>
//创建一个方法来设置绘制选择
公共无效setImageResource(){
的RelativeLayout的ImageIcon =(RelativeLayout的)mTabHost.getTabWidget()getChildAt(位置).findViewById(R.id.relIndicator)。
imageIcon.setBackgroundResource(R.drawable.tab_selector);
}
//现在在R.id.relIndicator通话setImageResource()方法的触摸事件
((RelativeLayout的)mTabHost.getTabWidget()。getChildAt(POSITION)
.findViewById(R.id.relIndicator))
.setOnTouchListener(新OnTouchListener(){
@覆盖
公共布尔onTouch(查看视图,MotionEvent事件){
setImageResources();
}
});
What i am doing::
- I have a fragment tabhost
- As shown in ths figure i want to remove the blue selector programatically if possible else other wise
Questions::
- Can this be performed ? ... If so How ?
- And onclick of tab i want to change the background
MainActivity.java
public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the view from main_fragment.xml
setContentView(R.layout.main_fragment);
// Locate android.R.id.tabhost in main_fragment.xml
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// Create the tabs in main_fragment.xml
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
// Create Tab1 with a custom image in res folder
mTabHost.addTab(mTabHost.newTabSpec("Rating").setIndicator("Rating"),
FragmentTab1.class, null);
// Create Tab2
mTabHost.addTab(mTabHost.newTabSpec("Price").setIndicator("Price"),
FragmentTab2.class, null);
// Create Tab3
mTabHost.addTab(mTabHost.newTabSpec("Distance").setIndicator("Distance"),
FragmentTab3.class, null);
}
}
解决方案
//to remove the strip below the tab you need to set the property of TabWidget
android:tabStripEnabled="false"
//create one layout tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relIndicator"
android:layout_width="@dimen/tab_width"
android:layout_height="@dimen/tab_height"
android:gravity="center_vertical"
android:clickable="true" >
<RelativeLayout
android:id="@+id/relTabIcon"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="30dip" >
<ImageView
android:id="@+id/inner_icon"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:clickable="true"
android:contentDescription="@string/app_name"
android:visibility="visible" />
</RelativeLayout>
<TextView
android:id="@+id/title"
style="@style/txtStyleWhite"
android:layout_width="wrap_content"
android:layout_height="25dip"
android:layout_below="@+id/relTabIcon"
android:layout_centerHorizontal="true"
android:paddingRight="2dp"
android:paddingLeft="2dp"
android:gravity="center"
android:text="MY TEXT"
android:textSize="@dimen/tab_font"
android:visibility="visible" />
</RelativeLayout>
//Create one method to set the drawable selector
public void setImageResource(){
RelativeLayout imageIcon = (RelativeLayout)mTabHost.getTabWidget().getChildAt(POSITION).findViewById(R.id.relIndicator);
imageIcon.setBackgroundResource(R.drawable.tab_selector);
}
//now on touch event of the R.id.relIndicator call setImageResource() method
((RelativeLayout) mTabHost.getTabWidget().getChildAt(POSITION)
.findViewById(R.id.relIndicator))
.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
setImageResources();
}
});
这篇关于在Android的定制fragmentTabhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文