我希望活动的右侧显示不同的项目,具体取决于左侧列表视图中的哪个项目。我能够按照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>
答案 0 :(得分:1)
您必须为该ListView设置和适配器。搜索有关创建自定义适配器类的教程,您可以找到其中的几十个。如果您计划为此平台开发,则必须了解自定义适配器。
但无论如何,你可以使用标准的ArrayAdapter来解决这个问题:
String[] countries = <put your strings here>
yourList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));
在此之后,为列表设置ItemClickListener,并根据需要在右侧窗格中处理内容更改。
答案 1 :(得分:0)
尝试类似
的内容<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
这里的重大变化是改变你所拥有的线性布局的layout_width,所以它们均匀地共享空间,我也将它们都放在水平线性布局中。
答案 2 :(得分:0)
来自http://www.anddev.org/view-layout-resource-problems-f27/two-listviews-side-by-side-t11199.html
几乎只需要将LinearLayout
中的内容与android:orientation="horizonal"
包装在一起。
<?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>