【问题标题】:Simple Layout Issue - Aligning menu button to far right, center简单布局问题 - 将菜单按钮对齐到最右边,居中
【发布时间】:2012-04-06 05:49:54
【问题描述】:

很难将图标挤到我想要的位置。我查看了 3 个不同的 Stack Overflow 问题,并尝试将图像放置在 RelativeLayout、LinearLayout 和 TableRow 中。所有这些都有各种 XML 选项。从来没有设法让图标向右移动并在分配空间的顶部和底部之间居中。在下面的图片中,红色是现在的位置,蓝色是我想要的位置。这是一张图片(它现在的样子,这是不正确的)和代码(imageView1 是我要对齐的):

还有代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/loginScrollView">
    <TableLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="vertical"
        android:padding="5dp">

        <ImageView android:id="@+id/logoImageView"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/logo" android:contentDescription="@string/app_name" />

        <TextView android:id="@+id/demoTextView"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="@string/logDetails" android:gravity="center_horizontal|center" />

        <EditText android:id="@+id/emailEditText"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:hint="@string/loginHint" android:imeOptions="actionNext"
            android:inputType="textEmailAddress" android:padding="20dp" />

        <EditText android:id="@+id/passEditText"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:hint="@string/password" android:imeOptions="actionDone"
            android:inputType="textPassword" android:padding="20dp" />

        <TableRow android:id="@+id/rememberTableRow"
            android:layout_width="wrap_content" android:layout_height="wrap_content">
            <TextView android:id="@+id/rememberTextView"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="@string/rememberDetails" android:layout_gravity="center" />

            <CheckBox android:id="@+id/rememberCheckBox"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:padding="20dp" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="showPopup"
                android:src="@drawable/abs__ic_menu_moreoverflow_holo_dark" android:layout_weight="1" android:layout_gravity="center|right"/>

        </TableRow>

        <TableRow android:id="@+id/buttonTableRow"
            android:layout_width="match_parent" android:layout_height="wrap_content">

            <Button android:id="@+id/registerButton" android:layout_width="0dip"
                android:layout_height="wrap_content" android:layout_gravity="bottom"
                android:layout_weight="1" android:text="@string/register" />

            <Button android:id="@+id/loginButton" android:layout_width="0dip"
                android:layout_height="wrap_content" android:layout_gravity="bottom"
                android:layout_weight="1" android:text="@string/login" />
        </TableRow>

    </TableLayout>
</ScrollView>

以下是我已经查看并尝试用作解决方案的 SO 问题: Aligning ImageView to right of the layout android Aligning with center in android with hierarchical Layouts Android ImageButton not displaying on right side of the screen

【问题讨论】:

  • 您需要帮助的图像视图在哪里?图片上有吗?
  • 是的,它就在复选框的右侧:它看起来像一个标准菜单图标(3 个垂直框)。
  • 啊,我刚到家,有人回答了哈哈
  • 好吧,我给了你一个有用的评论!
  • 谢谢夸奖!

标签: android android-layout


【解决方案1】:

据我所知,有多种选择。我给你两个:

第一个选项是在包含溢出图标的 ImageView 上设置 scaletype。你让它占用所有剩余的可用空间并将 scaletype 设置为fitEnd。这会将图标一直放在右侧。

<ImageView android:id="@+id/imageView1"
    android:layout_width="0dp" android:layout_height="wrap_content"
    android:layout_gravity="center_vertical" android:scaleType="fitEnd"
    android:layout_weight="1" android:onClick="showPopup"
    android:src="@drawable/abs__ic_menu_moreoverflow_normal_holo_dark" />

您可以通过在 CheckBox 上设置 1 的权重来实现相同的效果,这将使其带有溢出图标的 ImageView 一直“推”到右侧(感谢 TableRow 的行为类似于 LinearLayout )。然后,您可以简单地使 ImageView 包装其内容。不过,在这种情况下,您可能需要仔细检查可点击区域是否没有奇怪的副作用。

<TableRow android:id="@+id/rememberTableRow"
    android:layout_width="wrap_content" android:layout_height="wrap_content">

    <TextView android:id="@+id/rememberTextView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center" android:text="@string/rememberDetails" />

    <CheckBox android:id="@+id/rememberCheckBox"
        android:layout_width="0dp" android:layout_height="wrap_content"
        android:layout_weight="1" android:padding="20dp" />

    <ImageView android:id="@+id/imageView1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" android:onClick="showPopup"
        android:src="@drawable/abs__ic_menu_moreoverflow_normal_holo_dark" />

</TableRow>

【讨论】:

  • android:scaleTyle="fitEnd" 工作得很好。我实际上也删除了我曾尝试用来将菜单图像放入其中的 TableRow。我以前没见过 scaleType,非常感谢!
猜你喜欢
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
  • 2017-06-09
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
相关资源
最近更新 更多