[英]Android: LinearView with fixed sized widgets left and right and a flexible one in the middle
似乎是一個簡單的問題,但我無法弄清楚如何做到這一點。
我有一個水平的LinearView,在左邊有一些固定大小的東西,然后是一些文本應該盡可能地作為mich空間,但在右邊應該是另一個固定大小的對象。
現在,我的問題是現在:當中間的文本太長時,右邊的對象被推出窗口。 當文本太短時,它不會位於窗口的右邊界,而是直接位於文本旁邊。
我該怎么做? 到目前為止我的代碼(正如你所看到的,我試圖使用LayoutWeight,這沒有幫助......):
<?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">
<TextView
android:id="@+id/important"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
android:background="#000000"
android:layout_weight="1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingLeft="0dp"
android:textColor="#333333"
android:paddingBottom="0dp"
android:textSize="14sp"
android:scrollHorizontally="true"/>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666666"
android:paddingTop="3dp"
android:paddingRight="5dp"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:textSize="12sp" />
</LinearLayout>
<ImageView android:id="@+id/lastpage"
android:src="@drawable/lastpage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="200"
android:scaleType="center"
android:layout_gravity="right"/>
</LinearLayout>
您在兩個小部件( LinearLayout
第一個和第二LinearLayout
)上有android:layout_weight="1"
,這與您的業務規則不匹配。
就個人而言,我會在這里使用RelativeLayout
。 在第一個孩子上使用android:layout_alignParentRight="true"
,在第三個孩子上使用android:layout_alignParentLeft="true"
,在中間使用android:toLeftOf
和android:toRightOf
,將其錨定在外部固定子項之間。 這更明確地實現了您期望的業務規則。
看看這個答案Android:LinearView左右固定大小的小部件和中間的靈活小部件你可能只需要將中間元素的layout_width
設置為0px,使其不使用內容獲得的寬度,而是通過重量。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.