LinearLayout:左边的TextViews,右边的大ImageView

nr1*_*nr1 2 layout android

我正在开发一个LinearLayout但不幸的是它没有按预期工作.目标是在左侧使用带有两个TextView(一个放在另一个下面)的LinearLayout,在右侧放置一个ImageView.

ImageView应尽可能大,TextViews应占用剩余空间.

目前我的布局XML是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="@drawable/background" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="2dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/layout1label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:singleLine="true"
        android:text="1234"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/layout2label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1234_label2"
        android:textSize="14dp" />
</LinearLayout>


<ImageView
    android:id="@+id/layout_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_vertical"
    android:layout_margin="2dp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_launcher" />
Run Code Online (Sandbox Code Playgroud)

无效的部分:如果TextViews中的文本"太长",则ImageView会缩小.我完全想要它反过来.

有解决方案吗

Tod*_*der 5

使用它RelativeLayout代替更高效LinearLayout.然后您可以放置​​视图而无需嵌套布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/image"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    />
  <TextView
    android:id="@+id/subtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/image"
    android:layout_below="@+id/title"
    />
  <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

通过将TextViews相对于ImageView而不是相反地布置,ImageView空间优先,并且文本与其余部分一起工作.