【发布时间】:2014-07-23 16:25:58
【问题描述】:
首先,总的来说,我的 UI 很糟糕,这就是我需要帮助的原因。现在我有以下内容:
用画图解释:
我目前拥有的实际截图:
使用可在本文底部找到的代码。这是通过一些嵌套的 LineairLayouts 和权重来完成的。
我现在想要的是以下内容:
- ImageButton(宽度和高度已知/图像在xml中设置)
- TextView(高度已知,宽度应填充在 1 到 3 之间)
- TextView(高度已知,宽度(文本)未知)
- EditText(高度已知,宽度(文本)未知)
- AutoCompleteTextView(高度已知,宽度应在 4 到 9 之间填充)
- TextView(宽度和高度已知/文本在xml中设置)
- 微调器(高度已知,宽度应填充在 6 到 8 之间)
- ImageButton(宽度和高度已知/图像在 xml 中设置)这是我现在要添加的。
- 空格(宽度和高度都在代码中确定以填充空白)
我知道我可能能够弄清楚如何将这个 ImageButton 添加到另一个嵌套的 LineairLayout 和嵌套的权重中,但是由于我的应用程序的性能已经不是那么好,我目前正在尝试解决很多问题性能问题,我认为最好将这个 list_item.xml 转换为单个 RelativeLayout。
那么,我该怎么做呢?我只是在 UI 放置方面很糟糕,所以我会很感激我能得到的所有帮助。如何使用第二个 Paint-image 的结果创建 RelativeLayout?
当前代码:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xml>
<!-- The DOCTYPE above is added to get rid of the following warning:
"No grammar constraints (DTD or XML schema) detected for the document." -->
<!-- The View for a single CheckListItem -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- The TextViews -->
<LinearLayout
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin"
android:adjustViewBounds="true"
android:background="@layout/transparent_background"
android:contentDescription="@string/checkbox_content_description"
android:src="@drawable/checkbox_unchecked" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/default_margin"
android:layout_marginTop="@dimen/default_margin"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:id="@+id/tv_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true" />
</LinearLayout>
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/default_margin"
android:layout_marginTop="@dimen/default_margin" />
</LinearLayout>
<!-- The EditTexts -->
<LinearLayout
android:id="@+id/ll2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ll1"
android:gravity="center"
android:orientation="horizontal"
android:visibility="visible" >
<Space
android:id="@+id/filler_space_image"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/default_margin"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginRight="@dimen/default_margin" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/default_margin"
android:layout_marginRight="@dimen/default_margin"
android:orientation="vertical"
android:padding="0dp">
<EditText
android:id="@+id/et_result_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number" />
<TextView
android:id="@+id/tv_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tags"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/default_margin"
android:layout_marginRight="@dimen/default_margin"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="0dp"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/actv_result_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:inputType="text"
android:singleLine="true" />
<Spinner
android:id="@+id/sp_tags"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--<ImageButton
android:id="@+id/btn_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@android:drawable/ic_menu_manage"
android:contentDescription="@string/button_tags_content_description"
android:background="@layout/transparent_background" />-->
</LinearLayout>
<Space
android:id="@+id/filler_space_price"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/default_margin"
android:layout_marginRight="@dimen/default_margin" />
</LinearLayout>
</RelativeLayout>
编辑 1:
在我尝试了@AlexBalo suggestion 之后,它就可以工作了。只是android:layout_leftOf="@id/left_ll" 有问题。
PS:我的项目有两种不同的状态:一个未选中/绿色检查/红十字,仅显示视图 1、2 和 3。还有一个橙黄色检查,就像提供的图片一样。
这是 AlexBalo 迄今为止所做更改的结果:
状态未选中/绿色选中/红十字:
状态橙黄色检查:
使用以下代码:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xml>
<!-- The DOCTYPE above is added to get rid of the following warning:
"No grammar constraints (DTD or XML schema) detected for the document." -->
<!-- The View for a single CheckListItem -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/left_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@layout/transparent_background"
android:contentDescription="@string/checkbox_content_description"
android:src="@drawable/checkbox_unchecked"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin" />
<Space
android:id="@+id/filler_space_image"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin" />
</LinearLayout>
<TextView
android:id="@+id/tv_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/left_ll"
android:layout_toLeftOf="@+id/right_ll"
android:ellipsize="end"
android:singleLine="true"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin" />
<EditText
android:id="@+id/et_result_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_product_name"
android:layout_toRightOf="@id/left_ll"
android:inputType="number"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin" />
<AutoCompleteTextView
android:id="@+id/actv_result_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/right_ll"
android:layout_toRightOf="@id/et_result_amount"
android:layout_below="@+id/tv_product_name"
android:ellipsize="end"
android:inputType="text"
android:singleLine="true" />
<TextView
android:id="@+id/tv_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_result_amount"
android:layout_toRightOf="@id/left_ll"
android:text="@string/tags"
android:gravity="center"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin" />
<Spinner
android:id="@+id/sp_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/actv_result_name"
android:layout_toRightOf="@id/tv_tags"
android:layout_toLeftOf="@id/right_ll"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin" />
<LinearLayout
android:id="@id/right_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical">
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin" />
<Space
android:id="@+id/filler_space_price"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin"
android:layout_marginRight="@dimen/default_margin" />
<ImageButton
android:id="@+id/btn_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@android:drawable/ic_menu_manage"
android:contentDescription="@string/button_tags_content_description"
android:background="@layout/transparent_background"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin"
android:layout_marginRight="@dimen/default_margin" />
</LinearLayout>
</RelativeLayout>
我也遇到了一些问题,即 getView 在创建时被多次调用而不是只调用一次,但这是 another question 的问题。
【问题讨论】:
标签: android xml android-layout android-linearlayout android-relativelayout