[英]android: a tab host where each tab has an unread count badge
我需要编写一个标签栏,其中的每个tabspec都有一个指示器视图,该指示器视图包含一个可更改的数字标志。 想象一个选项卡式新闻阅读器,其中每个选项卡都有其自己的未读计数
我所做的是为标签指示符创建一个xml布局,将其填充到代码中,并将其与tabspec关联,例如:tab_indicator.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:paddingLeft="10dip"
android:paddingRight="10dip">
<FrameLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:paddingTop="5dip">
<ImageView android:src="@drawable/tab_selector_friends"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:text="2"
android:layout_width="10dip" android:layout_height="10dip"
android:textColor="@color/white_text"
android:textStyle="bold" android:typeface="monospace"
android:paddingLeft="1dip" android:paddingRight="1dip"
android:textSize="10dip" android:gravity="center" android:background="@drawable/temp_red_badge_big" />
</FrameLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Friends" />
</LinearLayout>
我的标签小部件(在MyTabWidget.java的创建方法上):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_menu);
TabHost tabHost = getTabHost();
Intent intent = new Intent().setClass(this, TodayActivity.class);
Resources resources = this.getResources();
TabHost.TabSpec spec = tabHost.newTabSpec(resources.getString(R.string.today));
View specIndicatorView;
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
specIndicatorView = inflater.inflate(R.layout.tab_indicator, null);
spec.setIndicator(specIndicatorView);
spec.setContent(intent);
tabHost.addTab(spec);
这种方法存在几个问题:
我的问题是,您能建议一种更好的方法吗? 有没有我没有考虑过的其他方法? 谢谢!
几件事。
使用RelativeLayout。 这可能对问题3有所帮助,因为它可以更好地控制这些元素在布局中的放置位置。
您应该将android:id属性与所有XML元素一起使用。 这样,您可以在xml中设置视图,但仍可以通过编程方式对其进行更改,这将有助于解决问题2。例如,如果要为TextView提供id属性,则无需对text属性进行硬编码。
在您的xml中,
<TextView android:id="count"
...
/>
在您的Java中,
TextView count = (TextView) findViewById(R.id.count);
count.setText(friendsCount);
总的来说,我不认为这是令人费解的,但是可以肯定地加以改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.