对话框内选项卡布局内的Android列表视图无法显示滚动条
我有一个扩展 Dialog 的自定义对话框类。里面有一个带有 2 个选项卡的选项卡布局。在每个选项卡中我都有一个列表视图。一切正常,但我无法显示滚动条。
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TabHost01"
android:layout_width="300dp"
android:layout_height="300dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<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="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"/>
<ListView
android:id="@+id/ListView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"/>
</FrameLayout>
</LinearLayout>
</TabHost>
这是我设置此功能的代码的一部分:
// get this window's layout parameters so we can change the position
WindowManager.LayoutParams params = getWindow().getAttributes();
// change the position. 0,0 is center
params.x = 0;
params.y = 250;
this.getWindow().setAttributes(params);
// no title on this dialog
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.myLayout);
// instantiate our list views for each tab
ListView listView01 = (ListView)findViewById(R.id.ListView01);
ListView listView02 = (ListView)findViewById(R.id.ListView02);
// instantiate and set our custom list view adapters
listViewAdapter01 = new ListViewAdapter01(context);
listView01.setAdapter(listViewAdapter01);
listViewAdapter02 = new ListViewAdapter02(context);
listView02.setAdapter(listViewAdapter02);
// get our tabHost from the xml
TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
tabs.setup();
// create tab 1
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.listView01);
tab1.setIndicator("List 1");
tabs.addTab(tab1);
// create tab 2
TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setContent(R.id.listView02);
tab2.setIndicator("List 01");
tabs.addTab(tab2);
I have a custom dialog class that extends Dialog. Inside this I have a Tab Layout with 2 tabs. In each tab I have a list view. Everything works but I can't get scroll bars to show up.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TabHost01"
android:layout_width="300dp"
android:layout_height="300dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<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="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"/>
<ListView
android:id="@+id/ListView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"/>
</FrameLayout>
</LinearLayout>
</TabHost>
and here is part of my code that sets this up:
// get this window's layout parameters so we can change the position
WindowManager.LayoutParams params = getWindow().getAttributes();
// change the position. 0,0 is center
params.x = 0;
params.y = 250;
this.getWindow().setAttributes(params);
// no title on this dialog
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.myLayout);
// instantiate our list views for each tab
ListView listView01 = (ListView)findViewById(R.id.ListView01);
ListView listView02 = (ListView)findViewById(R.id.ListView02);
// instantiate and set our custom list view adapters
listViewAdapter01 = new ListViewAdapter01(context);
listView01.setAdapter(listViewAdapter01);
listViewAdapter02 = new ListViewAdapter02(context);
listView02.setAdapter(listViewAdapter02);
// get our tabHost from the xml
TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
tabs.setup();
// create tab 1
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.listView01);
tab1.setIndicator("List 1");
tabs.addTab(tab1);
// create tab 2
TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setContent(R.id.listView02);
tab2.setIndicator("List 01");
tabs.addTab(tab2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OK,这里是自定义对话框类的完整工作代码,其中包含选项卡式布局,其中包含 listView。第一个选项卡有一个 listView,其中行是 textView 和 imageView,其中 imageView 右对齐。第二个选项卡有一个 listView,其中行是单个 textView。滚动条设置为较长的淡入淡出持续时间,以使其始终显示。对话框窗口本身设置为静态大小,以防止切换选项卡时对话框调整大小。对话框窗口也位于屏幕下方,而不是中央。 listView 使用自定义适配器,第二个选项卡的 listView 注册为上下文菜单。
我已将所有内容重命名为更通用,并且不包含我们产品的名称,因此在重命名时我可能犯了一些拼写错误,但我认为一切都是正确的。尽我所能地评论代码。希望这对一些人有帮助。
customDialog 的 XML (custom_dialog_layout.xml):
选项卡 1 listView 行 XML (list_view_01_row.xml)。这是一个左对齐的文本视图和一个右对齐的图像视图。 textView 已设置为更大的高度,以强制 listView 行更高。 listView 也被设置为特定的宽度,这会将 imageView 推到右侧以使其右对齐。
选项卡 2 listView 行 XML (list_view_02_row.xml)。与选项卡 1 相同,但只有一个 textView,没有 imageView。
最后是自定义 Dialog 类。
OK here is the complete working code for a custom dialog class that contains a tabbed layout which contains a listView. The first tab has a listView with rows being a textView and an imageView with the imageView being right aligned. The second tab has a listView with rows being a single textView. The scroll bars are set to a high fade duration to make them always show. The dialog window itself is set to a static size to prevent the dialog from resizing when switching tabs. The dialog window is also positioned lower on the screen, not in the center. The listViews use custom adapters and the second tab's listView is registered for a context menu.
I have renamed everything to be more generic and ont contain names of our product, so I may have made some typos when renaming but I think everything is right. Tried to comment the code as best I could. Hope this helps some people.
The customDialog's XML (custom_dialog_layout.xml):
Tab 1 listView row XML (list_view_01_row.xml). This is a textView, left aligned and an imageView, right aligned. The textView has been set to a larger height in order to force the listView rows to be higher. The listView has also been set to a specific width, this pushes the imageView to the right in order to right align it.
Tab 2 listView row XML (list_view_02_row.xml). Same as tab 1 but with a single textView, no imageView.
And finally the custom Dialog class.
尝试将布局 xml 中的所有
android:layout_width
属性设置为“fill_parent”
。Try to set all of the
android:layout_width
attributes within your layout xml to"fill_parent"
.