Android线性/相对布局 - 如何在中间"自动调整大小"对象(3个对象)

Pan*_*nos 7 android uitableview relativelayout android-layout android-linearlayout

我在android中有两个使用Linear(也尝试过相对)布局的情况.一个用于水平,另一个用于垂直.让我们从横向开始:

它是这样的:

<LinearLayout ... >   
    <Button ...  layout:gravity = "left" layout:width = "wrap_content"/>
    <TextView ... layout:width = ??????? />
    <Image  .... layout:gravity = "right" layout:width = "wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

好吧,我希望按钮保持在左侧,图像保持在右侧(坚持到最后,而不仅仅是文本视图的右侧)和textview(可能具有自动宽度或其他)保持在中间.如果我输入textview width ="fill/match_parent它将图像发送出屏幕.如果我放置wrap_content,那么图像不会停留在屏幕的右侧.我也尝试过相对布局而没有成功.

纵向相同的情况,我有类似的东西:

<LinearLayout ...>
    <LinearLayout .... layout:height = "wrap_content" layout:gravity= "top" />
    <ListView layout:height = ???????>
    <LinearLayout ... layout:height = "wrap_content" layout:gravity = "bottom" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这里要求相同.我希望第一个L.layout保持在顶部,列表视图之间的自动大小和第二个线性布局保持在底部.(想象一下,我正在尝试创建一个看起来像iPhone中的UITableView的视图,其中包含一个NavigationBar,项目列表和底部的工具栏.Fist LinearLayout是NavigationBar,LIst视图是单元格,第二个LinearLayout是工具栏).

有什么建议?更喜欢xml解决方案.

Moh*_*ikh 7

我们可以在这里使用RelativeLayout来完成它.

水平对齐

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">  

  <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="something"
    />

  <TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/image"
    android:layout_toRightOf="@+id/button" />

  <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@drawable/icon" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

垂直对齐

  <LinearLayout
    android:id="@+id/linear_top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

  <ListView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linear_top"
    android:layout_above="@+id/linear_bottom" />

  <LinearLayout
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)