【问题标题】:How to Align ImageButton left side and TextView Center using LinerLayout?如何使用 LinearLayout 对齐图像按钮左侧和 TextView 中心?
【发布时间】:2016-05-07 00:46:05
【问题描述】:
<?xml version="1.0" encoding="utf-8"?>  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ravi.designdemo.Main4Activity">  

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/navigationbar"
        android:id="@id/ll1">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imb1"
            android:background="@drawable/back" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:text="Sign In"
            android:textColor="#ffffff"
            android:gravity="center_vertical"
            android:textSize="25dp"
            android:textStyle="bold"/>

    </LinearLayout>

</RelativeLayout>

这是我的布局。我在左侧有一个 ImageView,在 LinearLayout 的中心有一个 TextView。我在 RelativeLayout 中使用 LinearLayout。

【问题讨论】:

  • 只需让 TextView 宽度匹配父项
  • 提供一些您想要的布局图像或草图。
  • 您可以为此目的使用android:layout_gravity 属性。自己玩价值。

标签: android layout textview imagebutton


【解决方案1】:

如果你的 LinearLayout 是垂直的

android:orientation="vertical"

然后你可以将 android::gravity 参数设置为 leftcenter

否则如果ImageButtonTextView在同一层级,那么就不需要使用LinearLayout,尤其是在里面 em>RelativeLayout 并使用相对布局参数将 ImageView 对齐到左侧,将 TextView 对齐到它的中心。

例如,我将删除 LinearLayout 并执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imb1"
            android:background="@drawable/back"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FF00"
            android:text="Sign In"
            android:textColor="#ffffff"
            android:textSize="25dp"
            android:layout_centerHorizontal="true"
            android:textStyle="bold"/>

</RelativeLayout>

这个解决方案的线索是使用

android:layout_centerHorizontal="true"

因为在 RelativeLayout 中这是将视图居中的正确方法。

【讨论】:

    【解决方案2】:

    android:layout_gravity="center_vertical" 将此添加到 Textview。

    【讨论】:

      【解决方案3】:

      在线性布局中,子元素一个接一个地出现。根据您的问题,您可以使用相对布局来做到这一点,但在线性布局中这是不可能的。

      <?xml version="1.0" encoding="utf-8"?>
      
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center_vertical"
          tools:context="com.example.ravi.designdemo.Main4Activity">
      
              <ImageButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/imb1"
                  android:layout_alignParentLeft="true"
                  android:background="@mipmap/ic_launcher" />
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/tv1"
                  android:text="Sign In"
                  android:textColor="#ffffff"
                  android:layout_centerInParent="true"
                  android:textSize="25dp"
                  android:textStyle="bold"/>
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 2015-03-17
        • 1970-01-01
        • 2022-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-15
        • 2021-09-20
        相关资源
        最近更新 更多