0

我在设计 Android 选项卡方面几乎不需要任何帮助。如下图所示,标签看起来很丑而且很宽。我怎样才能使它看起来更薄,因为我只想要一些没有太多空白空间的文本。

在此处输入图像描述

我的第二个问题显示在下图中,我正在从第一个选项卡的其他活动中加载数据,但它看起来非常变形,如下图所示。在其他活动中,我有 ScrollView。

在此处输入图像描述

4

1 回答 1

0

通过这种方法,您可以创建自定义选项卡

创建一个布局来表示 TabIndicator

这是 tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/tv_tabTitle"
              android:layout_width="fill_parent"
              android:layout_height="35dp"
              android:background="@drawable/bg_tab_indicator"
              android:gravity="center"
              android:textSize="15sp"
              android:textColor="@drawable/tv_tab_indicator_title"
              android:text="Tab title"
              />

这是 bg_tab_indicator.xml 可绘制对象

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Unselected tab -->
    <item android:state_focused="false"
          android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@drawable/bg_tab_indicator_unselected"/>

    <!-- Selected tab -->
    <item android:state_focused="false"
          android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@drawable/bg_tab_indicator_selected"/>

    <!-- Pressed tab -->
    <item android:state_pressed="true"
          android:drawable="@drawable/bg_tab_indicator_pressed"/>

    <!-- Selected tab using dial pad -->
    <item android:state_pressed="false"
          android:state_focused="true"
          android:state_selected="true"
          android:drawable="@drawable/bg_tab_indicator_selected"/>

</selector>

现在在你的 TabActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_activity);
    //if you are not using TabActivity then use findViewById(id) to find tabHost
    final TabHost tabHost = getTabHost();
    tabHost.setup();
    //if you are not using TabActivity then use findViewById(id) to find tabWidget
    TabWidget tabWidget = getTabWidget();
    tabWidget.setDividerDrawable(R.drawable.divider_tab);
    addTab("Tab1", tabHost, R.id.tab1);
    addTab("Tab2", tabHost, R.id.tab2);
    addTab("Tab3", tabHost, R.id.tab3);
}

这是 addTab 方法

private void addTab(String label, TabHost tabHost, int content){
    TabHost.TabSpec tabSpec = tabHost.newTabSpec(label)
           .setIndicator(indicatorView(label))
           .setContent(content);
    tabHost.addTab(tabSpec);
}

该方法生成indicatorView

private View indicatorView(String label){
    View view = layoutInflater.inflate(R.layout.tab_indicator, null);
    TextView textView = (TextView) view;
    textView.setText(label);
    return view;
}

希望对你有帮助

于 2012-09-18T17:00:33.783 回答