Android xml : stretch an element and wrap the other, 水平彼此相邻

标签 android xml

假设我有一个 EditText 和一个 Button 水平相邻。左边是 EditText,右边是 Button。

问题是,我希望按钮将其内容包裹起来,同时 EditText 从屏幕左侧一直延伸到按钮。因此按钮尽可能小(取决于按钮文本),EditText 尽可能宽(取决于按钮大小)

我不知道我是否错过了一个简单的技巧......但我该如何实现呢?

最佳答案

您必须使用RelativeLayout,使按钮右对齐并将EditText 设置为按钮的左侧。之后,EditText 的 fill_parent 和 Button 的wrap_content 将发挥作用。

看看下面的代码就可以明白一切。

<RelativeLayout
    android:id="@+id/RL1"
    ...
    android:orientation="horizontal" >

        <EditText
            android:layout_width="fill_parent"
            ...
            android:layout_toLeftOf="@+id/buttonRight" >
        </EditText>

        <Button
            android:id="@+id/buttonRight"
            android:layout_width="wrap_content"
            ...
            android:singleLine="true" 
            android:layout_alignParentRight="true">
        </Button>

</RelativeLayout>

关于Android xml : stretch an element and wrap the other, 水平彼此相邻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10644913/

相关文章:

java - 根据在 Spinner 中选择的值设置 imageView src

java - 调用 headlessfragment 时 Android Studio : Error of java. lang.NullPointerException

java - spring 错误 :org. springframework.web.context.ContextLoader - 上下文初始化失败

android - 将 android 自定义 View 绑定(bind)到特定的布局 xml(这甚至可能吗?)

xml - 如何在 XSLT 中对元素进行分组

java - CXF xml 配置未使用 disableCNcheck ="true"进行验证

android - LatLng 的标记未显示

android - 在 Android Studio 中使用 android-maps-utils

java - Java 中的 XPath 节点集

Android设备监视器: what is the syntax to filter rows by multiple tags?