gpt4 book ai didi

底部带有图标选择问题的 Android FragmentTabHost 选项卡

转载 作者:行者123 更新时间:2023-11-29 01:49:36 24 4
gpt4 key购买 nike

这两天我一直在为一个简单的用户界面而苦苦挣扎。我只想要底部带有选项卡的选项卡式布局(由于这个原因不能使用 Tabs ActionBarSherlock)。我还希望我的选项卡由图标和文本表示。很简单?我知道不建议在 android 中使用底部选项卡,但这是应用程序的要求。对此无能为力。

我设法编辑了 this example根据我的需要。现在我在底部有选项卡,可以使用 FragmentTabHost。在此之后,我尝试将图标添加到选项卡。如果我使用常规的 TabHost 我可以做到

mTabs.addTab(mTabs.newTabSpec("chapter").setIndicator("Chapter",getResources().
getDrawable(R.drawable.chapter1)), ContentFragment.class, null);

但我从这篇文章中了解到:FragmentTabHost with drawable icon显然它不适用于新的 FragmentTabHost。所以,关注这篇文章:Icon in Tab is not showing up ,我实现了一个自定义 View 来保存选项卡的图标和文本。而且效果很好。

这里的问题(正如其中一篇帖子中的评论所问)是选择不再存在。我看不到选择了哪个选项卡。

完整代码如下:

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);

}

Fragment1.java

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;
}

bottom_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>

tab1_view.xml

<?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!" />

tab1_icon.xml

<?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" />

最佳答案

尝试使用操作栏显示在底部。

示例代码

 @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" >

来源:http://developer.android.com/design/patterns/actionbar.html

关于底部带有图标选择问题的 Android FragmentTabHost 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19061295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com