【问题标题】:How to center the content inside a linear layout?如何在线性布局中居中内容?
【发布时间】:2013-08-05 18:21:55
【问题描述】:

我试图将ImageView 水平和垂直居中在LinearLayout 内,但我就是做不到。 我不使用RelativeLayout 的主要原因是因为我需要layout_weight(我的Activity 由四列组成,应该等分,并且还响应不同的屏幕宽度,每列都有一个ImageView 居中且未拉伸)。

到目前为止,这是我的 xml:

<LinearLayout 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"
    android:background="#000"
    android:baselineAligned="false"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_speak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_speak" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_readtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_readtext" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_edit" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_config"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_config" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 根据需要发布屏幕截图和当前屏幕截图

标签: android android-layout android-linearlayout


【解决方案1】:

android:gravity 处理其子元素的对齐,

android:layout_gravity 处理自身的对齐。

所以使用其中之一。

<LinearLayout 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"
    android:background="#000"
    android:baselineAligned="false"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imageButton_speak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_speak" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imageButton_readtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_readtext" />
    </LinearLayout>

    ...
</LinearLayout>

<LinearLayout 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"
    android:background="#000"
    android:baselineAligned="false"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_speak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_speak" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_readtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_readtext" />
    </LinearLayout>

    ...
</LinearLayout>

【讨论】:

  • "android:layout_gravity 会自行处理。"会修改为父级使用布局重力。
【解决方案2】:

android:layout_gravity 用于布局本身

android:gravity="center" 用于您的LinearLayout 的孩子

所以你的代码应该是:

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1" >

【讨论】:

    【解决方案3】:

    我尝试了这里提到的解决方案,但它没有帮助我。我介意的解决方案是 layout_width 必须使用 wrap_content 作为值。

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1" >
    

    【讨论】:

      【解决方案4】:

      这里有一些示例代码。这对我有用。

      <LinearLayout
          android:gravity="center"
          >
          <TextView
              android:layout_gravity="center"
              />
          <Button
              android:layout_gravity="center"
              />
      </LinearLayout>
      

      因此,您正在设计线性布局以将其所有内容(TextView 和 Button)放置在其中心,然后将 TextView 和 Button 相对于线性布局的中心放置。

      【讨论】:

      • 简单。 'gravity' 属性会影响视图的内容。那么在这种情况下,LinearLayout 的内容是什么?一个 TextView 和一个按钮。实际上,您是在指示布局管理器集中线性布局的任何内容。
      【解决方案5】:

      android:gravity 可用于 Layout 以对齐其子级。

      android:layout_gravity 可用于任何视图以使其自身与父视图对齐。

      注意:如果 self 或 children 未按预期居中,请检查宽度/高度是否为 match_parent 并更改为其他内容

      【讨论】:

        【解决方案6】:

        如果您正在寻找 java 代码中的答案,

        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setGravity(Gravity.CENTER);
        // add children
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-17
          • 2018-02-26
          • 1970-01-01
          • 2018-08-11
          • 1970-01-01
          • 2012-11-08
          • 2017-09-30
          相关资源
          最近更新 更多