Android线性/相对布局-如何自动调整中间对象的大小(3个对象) [英] Android Linear/Relative Layout - how to "auto size" object in the middle (3 objects)

查看:39
本文介绍了Android线性/相对布局-如何自动调整中间对象的大小(3个对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中有两个线性(也尝试过相对)布局的案例。一种是水平的,另一种是垂直的。让我们从水平开始:

类似于:

<LinearLayout ... >   
    <Button ...  layout:gravity = "left" layout:width = "wrap_content"/>
    <TextView ... layout:width = ??????? />
    <Image  .... layout:gravity = "right" layout:width = "wrap_content"/>
</LinearLayout>
我希望按钮保持在左侧,图像保持在右侧(紧贴在文本视图的末尾,而不仅仅是文本视图的右侧),文本视图(可能带有自动显示或其他功能)保持在中间。如果我输入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>

这里也有同样的要求。我希望第一个L布局留在顶部,列表视图自动大小之间的他们和第二个线性布局留在底部。(假设我正在尝试创建一个类似于iPhone中的UITableView的视图,该视图底部有一个导航栏、项目列表和一个工具栏。第一个LinearLayout是导航栏,列表视图是单元格,第二个LinearLayout是工具栏)。

有什么建议吗?更喜欢使用XML解决方案。

推荐答案

只需使用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>

垂直对齐

  <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>

这篇关于Android线性/相对布局-如何自动调整中间对象的大小(3个对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆