Android中的布局处理
问题描述:
我有一个自定义列表布局。列表中的内容应该是,Android中的布局处理
+--------------+-------------+------------+ |an image icon | text label |a checkbox | +--------------+-------------+------------+
- 图像应始终左对齐
- 复选框应始终右对齐。
- 文本标签应占用剩余空间。
我的问题是,复选框没有对齐到屏幕右侧。它根据文本标签的大小占据该位置。 如果标签太大,复选框从不显示/一半显示。
布局文件示例如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout android:id="@+id/tableLayout1"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TableRow android:id="@+id/tableRow1" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/imageView1" android:src="@drawable/icon"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TableLayout android:id="@+id/tableLayout2"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:text="TextView" android:id="@+id/textView1"
android:layout_height="wrap_content"></TextView>
</TableRow>
<TableRow android:id="@+id/tableRow6" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="TextView" android:id="@+id/textView2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</TableRow>
<TableRow android:id="@+id/tableRow7" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="TextView" android:id="@+id/textView3"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</TableRow>
<TableRow android:id="@+id/tableRow8" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="x"
android:id="@+id/textView4" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</TableRow>
</TableLayout>
<CheckBox android:text="" android:id="@+id/checkBox1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</TableRow>
</TableLayout>
</LinearLayout>
答
使用本
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativeLayout1">
<ImageView android:id="@+id/imageView1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:src="@drawable/icon" android:layout_alignParentTop="true"></ImageView>
<CheckBox android:id="@+id/checkBox1"
android:layout_alignParentTop="true" android:text="CheckBox"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true"></CheckBox>
<TextView android:text="@string/hello" android:id="@+id/TextView1"
android:layout_toRightOf="@id/imageView1" android:layout_toLeftOf="@id/checkBox1"
android:layout_height="wrap_content" android:layout_width="fill_parent"></TextView>
</RelativeLayout>
答
如果妳解决的TextView的宽度,则复选框会在正确的,或者如果u插入TextView的一些长文则仅乌尔复选框进来right.You给WRAP_CONTENT对于宽度多数民众赞成Ÿ它不会来正确....
另一种选择,就是你可以使用相对布局,并相应设置组件....
可能我的解决方案有助于ü。
万事如意
+0
感谢您的答复。修正textview的宽度可能会导致UI失真,因为android设备来自不同的分辨率。我会尝试相对布局。 – Tyrant
答
播放与layout_weight财产 并尝试此示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:src="@drawable/icon"
android:layout_height="wrap_content"
android:layout_weight="1">
</ImageView>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:text="some text 1"
android:layout_height="wrap_content">
</TextView>
<TextView
android:layout_width="fill_parent"
android:text="some text 2"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<CheckBox
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content">
</CheckBox>
</LinearLayout>
</LinearLayout>
这解决了我的问题。谢谢。 – Tyrant