如何制作一侧具有列表视图的分屏

问题描述

| 我希望活动的右侧显示不同的项目,具体取决于选择左侧列表视图的哪个项目。我能够按照Android ListView示例进行操作,其中listview占据了整个活动屏幕,但无法将其扩展为拆分屏幕。这是我的xml,左侧为列表视图,右侧为一个简单按钮。我在弄清楚将填充列表视图的代码时遇到了麻烦。 system_status.xml:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
  <LinearLayout
       xmlns:android=\"http://schemas.android.com/apk/res/android\"
       android:id=\"@+id/activity_system_status\"
       android:title=\"@string/system_status\"
       android:layout_width=\"fill_parent\"
       android:layout_height=\"fill_parent\"
       android:orientation=\"horizontal\">

    <LinearLayout
        android:layout_width=\"fill_parent\"
        android:layout_height=\"fill_parent\"
        android:orientation=\"vertical\"
        android:layout_weight=\"1\">

        <ListView
        android:id=\"@+id/my_list\"
        android:layout_width=\"fill_parent\"
        android:layout_height=\"fill_parent\">
        </ListView>

    </LinearLayout>

    <LinearLayout
        android:layout_width=\"fill_parent\"
        android:layout_height=\"fill_parent\"
        android:orientation=\"vertical\"
        android:layout_weight=\"1\">
        <ToggleButton android:text=\"ToggleButton\" android:id=\"@+id/toggleButton1\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"></ToggleButton>
    </LinearLayout>
    </LinearLayout>
更新: 我在Scythe的帖子的帮助下找到了答案。这是使它起作用的代码。上面的xml按原样工作。
public class SystemStatusActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ListView lv = (ListView) findViewById(R.id.my_list);
    String[] stats = getResources().getStringArray(R.array.array_system_status);
    lv.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,stats));
}
}
list_item.xml:
     <?xml version=\"1.0\" encoding=\"utf-8\"?>
<TextView xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"fill_parent\"
    android:padding=\"10dp\"
    android:textSize=\"16sp\" >
</TextView>
    

解决方法

您必须为该ListView设置和适配器。搜索有关创建自定义适配器类的教程,您可以找到许多此类。如果您打算为此平台开发,则绝对必须了解自定义适配器。 但是无论如何,您都可以使用标准的ArrayAdapter使其工作:
String[] countries = <put your strings here>
yourList.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,countries));
之后,为列表设置一个ItemClickListener,并根据需要在右窗格上处理内容更改。     ,尝试类似
<LinearLayout
    android:layout_width=\"fill_parent\" 
    android:layout_height=\"fill_parent\"
    android:orientation=\"horizontal\">
The above block was new
<LinearLayout
    android:layout_width=\"0dp\" <--BIG CHANGE HERE
    android:layout_height=\"fill_parent\"
    android:orientation=\"vertical\"
    android:layout_weight=\"1\">

    <ListView
    android:id=\"@+id/list\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"fill_parent\">

    </ListView>

</LinearLayout>

<LinearLayout
    android:layout_width=\"0dp\" Big change here
    android:layout_height=\"fill_parent\"
    android:orientation=\"vertical\"
    android:layout_weight=\"1\">
    <ToggleButton android:text=\"ToggleButton\" android:id=\"@+id/toggleButton1\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"></ToggleButton>
</LinearLayout>

</Linearlayout> NEW
这里最大的变化是更改了您拥有的linearlayout的layout_width,因此它们均匀地共享了空间,我也将它们都放置在水平线性布局中。     ,来自http://www.anddev.org/view-layout-resource-problems-f27/two-listviews-side-by-side-t11199.html 几乎只需要用
android:orientation=\"horizonal\"
将您所拥有的东西包装在
LinearLayout
中即可。
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
              android:orientation=\"horizontal\"
              android:layout_width=\"fill_parent\"
              android:layout_height=\"fill_parent\"
              >
    <LinearLayout android:orientation=\"vertical\"
                  android:layout_width=\"fill_parent\"
                  android:layout_height=\"fill_parent\"
                  android:layout_weight=\"1\"
                  >
        <ListView android:id=\"@+id/list\"
                  android:layout_width=\"wrap_content\"
                  android:layout_height=\"fill_parent\"
                  android:entries=\"@array/colors\"
                  />
    </LinearLayout>
    <LinearLayout android:orientation=\"vertical\"
                  android:layout_width=\"fill_parent\"
                  android:layout_height=\"fill_parent\"
                  android:layout_weight=\"1\"
                  >
        <TextView android:id=\"@+id/text\"
                  android:layout_width=\"wrap_content\" 
                  android:layout_height=\"fill_parent\" 
                  android:text=\"Hello World,AndroidTest\"
                  />
    </LinearLayout>
</LinearLayout>
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...