Android FragmentTabHost标签底部带有图标选择问题
我现在正在为一个简单的用户界面奋斗两天。 我只想要底部有选项卡的选项卡式布局(由于这个原因,无法使用选项卡Android FragmentTabHost标签底部带有图标选择问题,android,android-layout,tabs,icons,fragment-tab-host,Android,Android Layout,Tabs,Icons,Fragment Tab Host,我现在正在为一个简单的用户界面奋斗两天。 我只想要底部有选项卡的选项卡式布局(由于这个原因,无法使用选项卡ActionBarSherlock)。我还希望我的标签页由图标和文本表示。很简单?我知道不建议在android中使用底部标签,但这是应用程序的要求。对此无能为力 我设法根据我的需要进行编辑。现在我在底部有了选项卡,可以使用FragmentTabHost。在此之后,我尝试将图标添加到选项卡。如果我使用的是常规的TabHost,我就可以做到 mTabs.addTab(mTabs.newTabSp
ActionBarSherlock
)。我还希望我的标签页由图标和文本表示。很简单?我知道不建议在android中使用底部标签,但这是应用程序的要求。对此无能为力
我设法根据我的需要进行编辑。现在我在底部有了选项卡,可以使用FragmentTabHost
。在此之后,我尝试将图标添加到选项卡。如果我使用的是常规的TabHost
,我就可以做到
mTabs.addTab(mTabs.newTabSpec("chapter").setIndicator("Chapter",getResources().
getDrawable(R.drawable.chapter1)), ContentFragment.class, null);
但我从这篇文章中了解到:显然,它不适用于新的FragmentTabHost
。
所以,在这篇文章之后,我实现了一个自定义视图,用于保存选项卡的图标和文本。而且效果很好
这里的问题(正如一篇帖子中的评论所问)是选择不再存在了。我看不出选择了哪个选项卡
以下是完整的代码:
MainActivity.java
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//testing with custom view implementation to add icon
TabSpec spec = mTabHost.newTabSpec("tab" + 1);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab1_icon, null, false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText("Label 1");
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
Drawable myIcon = getResources().getDrawable( R.drawable.icon1 );
icon.setImageDrawable(myIcon);
icon.setScaleType(ImageView.ScaleType.FIT_CENTER);
spec.setIndicator(tabIndicator);
mTabHost.addTab(spec,
Fragment1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("Contacts"), Fragment2.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
Fragment3.class, null);
}
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.tab1_view, null);
return v;
}
}
Fragment1.java
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//testing with custom view implementation to add icon
TabSpec spec = mTabHost.newTabSpec("tab" + 1);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab1_icon, null, false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText("Label 1");
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
Drawable myIcon = getResources().getDrawable( R.drawable.icon1 );
icon.setImageDrawable(myIcon);
icon.setScaleType(ImageView.ScaleType.FIT_CENTER);
spec.setIndicator(tabIndicator);
mTabHost.addTab(spec,
Fragment1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("Contacts"), Fragment2.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
Fragment3.class, null);
}
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.tab1_view, null);
return v;
}
底部的tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#FF0000"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1!" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="55" >
<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:adjustViewBounds="false"
android:src="@drawable/icon1" />
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:gravity="center_horizontal" />
tab1_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#FF0000"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1!" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="55" >
<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:adjustViewBounds="false"
android:src="@drawable/icon1" />
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:gravity="center_horizontal" />
tab1_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#FF0000"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1!" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="55" >
<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:adjustViewBounds="false"
android:src="@drawable/icon1" />
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:gravity="center_horizontal" />
尝试使用actionbars在底部显示 示例代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Create an actionbar menu
menu.add("Set as Wallpaper")
// Add a new Menu Button
.setOnMenuItemClickListener(this.SetWallpaperClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onCreateOptionsMenu(menu);
}
// Capture actionbar menu item click
OnMenuItemClickListener SetWallpaperClickListener = new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// Retrieve a WallpaperManager
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(MainActivity.this);
try {
// Change the current system wallpaper
myWallpaperManager.setResource(R.drawable.wallpaper);
// Show a toast message on successful change
Toast.makeText(MainActivity.this,
"Wallpaper successfully changed", Toast.LENGTH_SHORT)
.show();
} catch (IOException e) {
// TODO Auto-generated catch block
}
return false;
}
};
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:uiOptions="splitActionBarWhenNarrow" >
尝试使用:
如果您想要支持api lower 11,只需将TabHost更改为FragmentTabHost(从support lib或Sherlock)不要使用底部的选项卡栏:也可以看到12:00左右的标记。我已经完成了这一步。但客户的要求是底部要有钢筋。对此无能为力…如果标签也在顶部,我的问题中所述的问题将持续存在…你有解决方案吗??如果是的话,请把它贴在这里。即使我也面临着同样的问题:(这只在操作系统认为宽度足够窄时才起作用。可能在纵向上起作用,但在横向上肯定不行。)