我现在已经搞这两天了,但似乎仍然无法弄明白。
我终于在我的所有活动中都可以看到tabhost,但为了做到这一点,我必须创建一个片段活动。这搞砸了我的标签按钮的布局。当我使用tab活动时,它工作得很好但现在看起来像这样:
我希望蓝色按钮占据整个栏。现在没有像现在这样的背景空间。我不知道这是布局问题还是缩放问题?这是我创建选项卡的代码,也是我调整tabe大小的位置,以便中间选项卡的长度是外部选项卡的两倍:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
// the following code is used to decrease the size of the two end tabs
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
TabHost mth = (TabHost)findViewById(android.R.id.tabhost);
mth.getTabWidget()
.getChildAt(0)
.setLayoutParams(
new LinearLayout.LayoutParams(width / 4,
(int) (width / 4 * .625)));
mth.getTabWidget()
.getChildAt(1)
.setLayoutParams(
new LinearLayout.LayoutParams(width / 2,
(int) (width / 4 * .625)));
mth.getTabWidget()
.getChildAt(2)
.setLayoutParams(
new LinearLayout.LayoutParams(width / 4,
(int) (width / 4 * .625)));
}
/** (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
super.onSaveInstanceState(outState);
}
/**
* Initialise the Tab Host
*/
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
Resources res = getResources();
fragactivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("", res.getDrawable(R.drawable.tab_home)), ( tabInfo = new TabInfo("Tab1",ArrowsActivity.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
fragactivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("", res.getDrawable(R.drawable.tab_search)), ( tabInfo = new TabInfo("Tab2", OptionsActivity.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
fragactivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("", res.getDrawable(R.drawable.twitter)), ( tabInfo = new TabInfo("Tab3", ArrowsActivity.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
// Default to first tab
this.onTabChanged("Tab1");
//
mTabHost.setOnTabChangedListener(this);
}
/*
* @param activity
* @param tabHost
* @param tabSpec
* @param clss
* @param args
* */
private static void addTab(fragactivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
// Attach a Tab view factory to the spec
tabSpec.setContent(activity.new TabFactory(activity));
String tag = tabSpec.getTag();
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.detach(tabInfo.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
tabHost.addTab(tabSpec);
}
这是旧代码(当按钮大小正确时。)看起来视图正在通过另一个.xml文件传递,该文件指定图标应该与父代相匹配。我尝试在新代码中复制它,但我认为这对我来说有点太复杂了。这是与.xml一起使用的代码:
private void setTabs() {
// add tabs
addTab("Home", R.drawable.tab_home, ArrowsActivity.class);
addTab("Attendance", R.drawable.tab_search, OptionsActivity.class);
addTab("@PSUStrength", R.drawable.twitter, OptionsActivity.class);
// addTab("Contact Us", R.drawable.contact, OptionsActivity.class);
}
private void addTab(String labelId, int drawableId, Class<?> c) {
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
// TextView title = (TextView) tabIndicator.findViewById(R.id.title);
// title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
// registerForContextMenu(icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
tab_indicator.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical">
<ImageView android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:src="@drawable/icon"
/>
</RelativeLayout>
我真的很感激这方面的一些帮助。感谢。
答案 0 :(得分:1)
您好我的文字标签有问题,而不是图片,但我认为该解决方案适用于这两种情况。
在TabWidget内部,您先前输入的每个指标都有一个LinearLayout(在下面的代码中查看childAt)。此LinearLayout充当指标的文本/图像的容器。默认情况下,此LinearLayout具有一些填充。在我的情况下,这是24.我只是降低它。
将TabSpec实例添加到TabHost后,输入以下代码
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
View childAt = mTabHost.getTabWidget().getChildAt(i);
childAt.setPadding(childAt.getPaddingLeft()/2,
childAt.getPaddingTop(),
childAt.getPaddingRight()/2,
childAt.getPaddingBottom());
}
保持这种动态是一个很好的建议,这就是为什么我给出之前的一半。如果设置固定数字,则可能会遇到其他(较小)分辨率的问题。
如果您的标签上有文字,您可能也想要操纵它。这样做:
TextView tv = (TextView) childAt.findViewById(android.R.id.title);
//do whatever change in tv you might need, eg.
tv.setSingleLine();