【问题标题】:TextView between two elements两个元素之间的TextView
【发布时间】:2018-09-05 18:35:20
【问题描述】:

我有一个 Android 应用并创建了一个视图。该视图包含RelativeLayout,它包含3 个子元素:CheckBox、TextView 和Button。 CheckBox 放置在左侧父级,Button 放置在右侧,TextView 在这些元素之间。 TextView 可以包含任意长度的文本,如果文本很长,TextView 不能与 ChecBox 和 Button 重叠。现在看起来像这样:

但我想要这个:

是否可以正确放置元素?

【问题讨论】:

  • 发布你的 layout.xml
  • 一个包含 3 个视图的线性布局应该可以解决问题。只需记住将 textview 的权重设置为 1 并将宽度设置为 0
  • 使用线性布局而不是相对布局。
  • 将 android:layout_toLeftOf= 属性添加到您的复选框
  • 您可以使用 2 个元素(复选框和按钮)来归档该设计

标签: android android-layout


【解决方案1】:

你可以用这个

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="button" />

    <TextView
        android:text="this is my test this is my test this is my test this is my test this is my test "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/checkBox"
        android:layout_toLeftOf="@id/button"
        android:layout_toRightOf="@id/checkBox"
        android:layout_toStartOf="@id/button" />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    包装 3 个视图的线性布局(而不是相对布局)应该可以解决问题。只需记住将 textview 的权重设置为 1 并将宽度设置为 0

    <LinearLayout  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  />
    
    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:weight="1"  />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout 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"
      tools:context=".MainActivity">
      
      <CheckBox
          android:id="@+id/checkBox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="8dp"
          android:layout_marginStart="8dp"
          app:layout_constraintBottom_toBottomOf="@+id/textView"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="@+id/textView" />
      
      <TextView
          android:id="@+id/textView"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginLeft="8dp"
          android:layout_marginRight="8dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:text="Helovusndijfsdufsdjfkjdsbfkjsdkjfbskjdbfkjsbdfkjbskdjfbskjbdfkjbfsdf"
          app:layout_constraintBottom_toBottomOf="@+id/button"
          app:layout_constraintEnd_toStartOf="@+id/button"
          app:layout_constraintHorizontal_bias="0.005"
          app:layout_constraintStart_toEndOf="@+id/checkBox"
          app:layout_constraintTop_toTopOf="@+id/button" />
      
      <Button
          android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginEnd="8dp"
          android:layout_marginRight="8dp"
          android:layout_marginTop="8dp"
          android:text="Button"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintTop_toTopOf="parent" />
      
      </android.support.constraint.ConstraintLayout>
      

      这就是它的样子:

      【讨论】:

        【解决方案4】:

        使用 ConstraintLayout 你可以得到这个:

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
        
                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
        
                    <CheckBox
                        android:id="@+id/checkBox"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="8dp"
                        app:layout_constraintBottom_toBottomOf="parent" />
        
                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:text="this is a long text bla bla bla la this is a long text bla bla bla la"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/button2"
                        app:layout_constraintStart_toEndOf="@+id/checkBox"
                        app:layout_constraintTop_toTopOf="parent" />
        
                    <Button
                        android:id="@+id/button2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:text="Button"
                        app:layout_constraintEnd_toEndOf="parent" />
        
        
                </android.support.constraint.ConstraintLayout>
            </LinearLayout>
        

        结果:

        【讨论】:

          【解决方案5】:

          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
          
              <CheckBox
                  android:id="@+id/checkBox"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
          
              <TextView
                  android:layout_toRightOf="@id/checkBox"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim"
                  android:layout_toLeftOf="@id/btn" />
          
              <Button
                  android:id="@+id/btn"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" 
                  android:text="button"
                  android:layout_alignParentRight="true" />
          
          </RelativeLayout>
          

          【讨论】:

          • 您的复选框和按钮的重力方向错误。它不是垂直|中心
          【解决方案6】:

          您可以简单地将weight 添加到 textview 它将自动调整

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:gravity="center_vertical" android:layout_width="match_parent"
              android:orientation="horizontal"
              android:layout_height="wrap_content">
          
              <CheckBox
                  android:id="@+id/checkBox"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"   />
          
              <TextView
                  android:id="@+id/textView"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="large text view that not overlay neither checkbox nor button" />
          
              <Button
                  android:id="@+id/button"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Button" />
          </LinearLayout>
          

          【讨论】:

            【解决方案7】:

            试试这个 XML 布局:

            <?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"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
            
                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
            
                    <CheckBox
                        android:id="@+id/checkBox"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="8dp"
                        app:layout_constraintBottom_toBottomOf="parent" />
            
                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:text="Some Long text Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla "
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/button2"
                        app:layout_constraintStart_toEndOf="@+id/checkBox"
                        app:layout_constraintTop_toTopOf="parent" />
            
                    <Button
                        android:id="@+id/button2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:text="Button"
                        app:layout_constraintEnd_toEndOf="parent" />    
                </android.support.constraint.ConstraintLayout>
            </LinearLayout>
            

            希望对你有所帮助。

            【讨论】:

              【解决方案8】:

              不需要在checkBox和Button之间放置Textview, 您可以使用 2 个元素(复选框和按钮)。 你必须做什么 只需将文本设置为复选框,然后设置按钮。

              为了更好地理解,请阅读以下代码:

              <LinearLayout
                  android:gravity="center"
                  android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
              
                  <CheckBox
                      android:text="Some bla blablablablablablabla bla bla bla bla bla bla "
                      android:layout_width="0dp"
                      android:layout_weight="0.7"
              
                      android:layout_height="wrap_content" />
                  <Button
                      android:text="Button"
                      android:layout_width="0dp"
                      android:layout_weight="0.3"
                      android:layout_height="wrap_content" />
              </LinearLayout>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2012-01-01
                • 1970-01-01
                • 2020-02-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多