【问题标题】:Horizontal LinearLayout with three views, middle view should be horizontally centered三个视图的Horizo​​ntal LinearLayout,中间视图应该水平居中
【发布时间】:2019-01-04 19:47:59
【问题描述】:

我在水平LinearLayout 中有三个项目:一个TextView、一个ImageButton 和另一个TextView。这三个都是垂直居中的。

无论两个TextViews 的宽度是多少,我怎样才能另外实现中间ImageButton 完全水平居中?

也就是说:我想让ImageButton 位于正中心,左边的TextView 应该看起来是右对齐的,右边的TextView 应该看起来是左对齐的。所有三个视图都应垂直居中。

谢谢。

斯蒂芬

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    在线性布局中使用:

    android:weightSum=“3”
    

    并在您的 3 个布局中的每个布局中使用以下属性

    android:layout_weight=“1”
    android:layout_width=“0dp”
    

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3"
    tools:context=".MainActivity">
    
    <TextView
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    
    <ImageButton
        android:layout_weight="1"
        android:layout_width="0"
        android:layout_height="wrap_content" />
    
    <TextView
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    
    
    
    </LinearLayout>
    

    请查看Android官方文档以查看LinearLayout的更多属性: https://developer.android.com/reference/android/widget/LinearLayout

    【讨论】:

      【解决方案2】:

      你最好为他们设定权重。只是玩重量。

      如果它们具有恒定的重量,那么无论如何它们总是会填充特定数量的空间。

      1-设置所有视图宽度以匹配父级。

      2- 将所有权重设置为 1。

      然后确保您的图像位于这些文本视图的中心

       <EditText
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1"
              android:gravity="top"
              android:hint="@string/message" />
      

      【讨论】:

        【解决方案3】:

        孩子们必须有:

        1 - layout_width="0dp"

        2 - layout_weight="1"

        这是您需要的实现:

        <?xml version="1.0" encoding="utf-8"?>
        <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">
        
            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="This is a first text" />
        
            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                tools:srcCompat="@drawable/avatar" />
        
            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="This is second text that has a long content than the first one" />
        
        </LinearLayout>
        

        【讨论】:

          【解决方案4】:

          使用布局权重。检查以下示例:

          <?xml version="1.0" encoding="utf-8"?>
              <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/layout_cancel_x_next"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
          
                 <TextView
                     android:id="@+id/text1"
                     android:layout_width="@dimen/dimen_0dp"
                     android:layout_weight="1"
                     android:gravity="center_vertical"
                     android:layout_height="match_parent"
                     android:text="text1"
                     />
                  <ImageView
                      android:id="@+id/b"
                      android:layout_width="@dimen/dimen_0dp"
                      android:layout_height="wrap_content"
                      android:gravity="center_horizontal"
                      android:src="@drawable/ic_delete_button"
                      android:layout_weight="1"
                      />
                  <TextView
                      android:id="@+id/text2"
                      android:layout_width="@dimen/dimen_0dp"
                      android:layout_weight="1"
                      android:layout_height="match_parent"
                      android:text="text2"/>
          
              </LinearLayout>
          

          【讨论】:

            【解决方案5】:

            请试试这个...

            <?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical">
            
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="end"
                    android:text="This is a first text" />
            
                <ImageButton
                    android:id="@+id/imageButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/ic_launcher" />
            
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="start"
                    android:text="This is second text" />
            </LinearLayout>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-09-12
              • 1970-01-01
              • 1970-01-01
              • 2019-10-21
              • 2012-06-06
              • 2012-09-04
              • 1970-01-01
              • 2017-02-26
              相关资源
              最近更新 更多