在android选项卡栏中为每个选项卡指定宽度

时间:2012-10-10 05:44:13

标签: android android-layout tabs android-widget

您好我必须开发一个标签栏示例。我必须设置每个标签的宽度。因为我希望设置第一个标签宽度更大第二个标签宽度很小。我可以开发。请帮助我。

这是我的代码部分:

main.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 个答案:

答案 0 :(得分:2)

可能会有所帮助

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

其中width和height是该标签的所需大小。

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

答案 1 :(得分:0)

使用android:layout_weight给出特定标签的这种外观。

答案 2 :(得分:0)

据我所知,您可以使用ImageViews创建单独的xml,将其扩展为View并将View视为指示器,而不是直接将drawable设置为指示器。这样,您可以为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>

通过这种方式,您可以为仪表板和订单分别使用xmls。

希望这会有所帮助。

答案 3 :(得分:0)

对于Xamarin开发人员

AddView(_tabLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent));