【问题标题】:How can I align a TextView to the far left and ImageView to the far right in an Actionbar Spinner?如何在 Actionbar Spinner 中将 TextView 与最左侧对齐,将 ImageView 与最右侧对齐?
【发布时间】:2014-11-11 22:16:59
【问题描述】:

我正在尝试布局操作栏微调器下拉列表。我在每一行都有一个 textview 和一个 imageview 。我希望文本视图与下拉列表的最左侧对齐(使用 paddingLeft),并且我希望图像视图与下拉列表的最右侧对齐。文本视图中的文本长度不同,因此图标最终与文本视图的右侧对齐。此布局在操作栏上提供了一个包含微调器标题和所选项目名称的两行项目。

这是我目前的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/spinner_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

    <LinearLayout
        android:id="@+id/item_row"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="-5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:layout_weight="1"
            android:gravity="left"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:contentDescription="icon" />
    </LinearLayout>
</LinearLayout>

我尝试了类似下面的相对布局,结果相似(见屏幕截图)。蓝牙图标代表尚未创建的图标(相同大小):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/spinner_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="14sp" />

<RelativeLayout
    android:id="@+id/item_row"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/item_name"
        android:contentDescription="icon" />
</RelativeLayout>

【问题讨论】:

    标签: android android-layout layout android-actionbar spinner


    【解决方案1】:

    使用RelativeLayout 而不是LinearLayout,并使用您孩子的 alignParentLeft/Right 属性来正确放置它们。

    【讨论】:

    • 你能发布你尝试的RelativeLayout吗?
    • 我已将其添加到原始问题中
    • 删除android:layout_toRightOf="@+id/item_name"这一行。它和alignParentRight 的组合告诉系统将图像的开头立即放在文本的右侧,并将结尾放在容器的右侧。您可以在屏幕截图中看到它;图像占用了所有剩余空间。或者,如果您想避免文本和图像重叠,您可以告诉文本视图将自身布局到图像视图的左侧。这将使 textview 占用剩余空间。希望有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2017-04-27
    • 2016-07-11
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多