0

嗨,我必须开发一个标签栏示例。在这里我必须为每个标签设置宽度。因为我希望将第一个标签宽度设置为更大。第二个标签宽度很小。我该如何开发。请帮助我。

这是我的代码部分:

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:background="#ffffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:tabStripEnabled="false"

          android:background="@drawable/tabicon" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
  </LinearLayout>
  </TabHost>

TabBar.java:

public class TabBarSimple extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);



    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    TabSpec dbspec = tabHost.newTabSpec("DashBoard"); 
    dbspec.setIndicator("Dashboard", getResources().getDrawable(R.drawable.dashboard));


    Intent dbIntent = new Intent(this, PhotosActivity.class);
    dbspec.setContent(dbIntent);
    tabHost.addTab(dbspec);

    TabSpec orderspec = tabHost.newTabSpec("Orders");
    orderspec.setIndicator("Orders", getResources().getDrawable(R.drawable.orders));

    Intent orderIntent = new Intent(this, SongsActivity.class);
    orderspec.setContent(orderIntent);
    tabHost.addTab(orderspec);

    }

}

在这里我如何在每个选项卡上设置我的宽度。

4

4 回答 4

2

可能这会有所帮助

   tabHost.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(width,height));

其中宽度和高度是该选项卡所需的大小。

您需要通过调用 getChildAt() 的适当索引对每个选项卡重复此操作。

于 2014-05-05T06:00:52.960 回答
0

根据我的说法,您可以使用 ImageViews 创建单独的 xml,将它们膨胀为 View 并将 View 设置为指示器,而不是直接将 drawables 设置为指示器。这样,您可以为 3 个 ImageView 设置不同的宽度。

TabBar.java

View view = LayoutInflater.from(context).inflate(R.layout.indicator_view_db,null);
dbspec.setIndicator(view);

indicator_view_db.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:src="@drawable/dashboard"/>

</RelativeLayout>

通过这种方式,您可以为仪表板和订单拥有单独的 xml。

希望这可以帮助。

于 2012-10-10T07:01:04.913 回答
0

用于android:layout_weight赋予此类特定选项卡的外观。

于 2012-10-10T05:46:34.537 回答
0

对于 Xamarin 开发人员

AddView(_tabLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent));
于 2016-02-16T09:27:58.140 回答