Android:tabactivity - 所有选项卡的内容首先重叠
我正在测试 TabActivity,每个选项卡中都有一个列表。
运行应用程序时,选项卡的内容会像这样重叠。
单击选项卡后,重叠会被清除。 这是我的代码:
testtabs.xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
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" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</FrameLayout>
</LinearLayout>
</TabHost>
和测试活动
public class TabbedActivity extends TabActivity {
private static final String LIST1_TAB_TAG = "List1";
private static final String LIST2_TAB_TAG = "List2";
private ListView listView1;
private ListView listView2;
private TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testtabs);
tabHost = getTabHost();
// setup list view 1
listView1 = (ListView) findViewById(R.id.list1);
// create some dummy strings to add to the list
List<String> list1Strings = new ArrayList<String>();
list1Strings.add("List 11");
list1Strings.add("List 12");
list1Strings.add("List 13");
list1Strings.add("List 14");
listView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
// setup list view 2
listView2 = (ListView) findViewById(R.id.list2);
List<String> list2Strings = new ArrayList<String>();
list2Strings.add("Test2 List 21");
list2Strings.add("Testing 22");
list2Strings.add("More test 23");
list2Strings.add("Test Again 24");
listView2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list2Strings));
// add views to tab host
tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView1;
}
}));
tabHost.addTab(tabHost.newTabSpec(LIST2_TAB_TAG).setIndicator(LIST2_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView2;
}
}));
tabHost.setCurrentTab(0);
}
}
I was testing TabActivity with a list in each tab.
While running the app, the contents of the tabs gets overlapped like this.
After i click on the tabs the overlapping gets cleared.
Here is my code :
testtabs.xml layout :
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
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" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</FrameLayout>
</LinearLayout>
</TabHost>
And Test Activity
public class TabbedActivity extends TabActivity {
private static final String LIST1_TAB_TAG = "List1";
private static final String LIST2_TAB_TAG = "List2";
private ListView listView1;
private ListView listView2;
private TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testtabs);
tabHost = getTabHost();
// setup list view 1
listView1 = (ListView) findViewById(R.id.list1);
// create some dummy strings to add to the list
List<String> list1Strings = new ArrayList<String>();
list1Strings.add("List 11");
list1Strings.add("List 12");
list1Strings.add("List 13");
list1Strings.add("List 14");
listView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
// setup list view 2
listView2 = (ListView) findViewById(R.id.list2);
List<String> list2Strings = new ArrayList<String>();
list2Strings.add("Test2 List 21");
list2Strings.add("Testing 22");
list2Strings.add("More test 23");
list2Strings.add("Test Again 24");
listView2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list2Strings));
// add views to tab host
tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView1;
}
}));
tabHost.addTab(tabHost.newTabSpec(LIST2_TAB_TAG).setIndicator(LIST2_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView2;
}
}));
tabHost.setCurrentTab(0);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从 xml 布局中删除这两个 ListView,然后在 java 代码中创建它们。
例如 listView1 = new ListView(this);
一切都会好起来的。
You can remove both ListView from xml layout and just create them in java code.
e.g. listView1 = new ListView(this);
Everything will be Ok.
我发现的另一个解决方案是将以下标记添加到布局 XML 文件中的两个列表视图中:
Another solution I found is to add the following tag to both the listviews in the layout XML file:
您在 FrameLayout 中使用了两个 ListView,这就是重叠的原因。
如果你希望你应该有一个ListView在另一个ListView下面,将ListView保留在LinearLayout中,如下所示,
You are taking two ListView's in a FrameLayout that is the reason for your over-lapping.
If you want that you should have one ListView below the other keep the ListView's inside the LinearLayout like this,