【问题标题】:How to use margin on checkboxes so they will be on each other如何在复选框上使用边距,使它们彼此位于
【发布时间】:2020-05-29 23:24:27
【问题描述】:

我有以下代码:

    <LinearLayout
        android:id="@+id/availability_sunday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tile_divider"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text_sunday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sunday:" />

        <CheckBox
            android:id="@+id/checkbox_sunday_morning"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:layout_width="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp" />

        <TextView
            android:id="@+id/text_sunday_morning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Morning"/>

        <CheckBox
            android:id="@+id/checkbox_sunday_evening"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:layout_width="wrap_content" />

        <TextView
            android:id="@+id/text_sunday_evening"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Evening"/>
    </LinearLayout>

RelativeLayout 中。我希望布局是:

Sunday     [] Morning [] Evening
Monday     [] Morning [] Evening
...
Thursday   [] Morning [] Evening

但是如果我在星期一添加相同的块,我会得到:

因为工作android:layout_marginStartandroid:layout_marginLeft。我希望复选框在彼此之上。我该怎么做?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    试试下面的代码:

    <LinearLayout
       android:layout_below="@+id/tile_divider"
        android:id="@+id/availability_sunday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/text_sunday"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Sunday:" />
    
        <CheckBox
            android:id="@+id/checkbox_sunday_morning"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:layout_width="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp" />
    
        <TextView
            android:id="@+id/text_sunday_morning"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Morning"/>
    
        <CheckBox
            android:id="@+id/checkbox_sunday_evening"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:layout_width="wrap_content" />
    
        <TextView
            android:id="@+id/text_sunday_evening"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Evening"/>
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      我对 Android Studio 了解不多,但试试这个:
      -- 创建一个表格布局
      -- 将所有数据(textView 等)放入其中。

      目标是保持数据对称(我认为)。

      Also check out this

      【讨论】:

        【解决方案3】:

        尝试约束布局会更容易..

        <androidx.constraintlayout.widget.ConstraintLayout 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="match_parent">
        <!--For sunday-->
        <TextView
            android:id="@+id/text_sunday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:text="Sunday:"
            app:layout_constraintBottom_toBottomOf="@id/checkbox_sunday_morning"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/checkbox_sunday_morning" />
        
        <CheckBox
            android:id="@+id/checkbox_sunday_morning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:drawSelectorOnTop="true"
            app:layout_constraintStart_toEndOf="@id/text_sunday"
            app:layout_constraintTop_toTopOf="parent" />
        
        <TextView
            android:id="@+id/text_sunday_morning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="Morning"
            app:layout_constraintBottom_toBottomOf="@id/checkbox_sunday_morning"
            app:layout_constraintStart_toEndOf="@id/checkbox_sunday_morning"
            app:layout_constraintTop_toTopOf="@id/checkbox_sunday_morning" />
        
        <CheckBox
            android:id="@+id/checkbox_sunday_evening"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:drawSelectorOnTop="true"
            app:layout_constraintStart_toEndOf="@id/text_sunday_morning"
            app:layout_constraintTop_toTopOf="parent" />
        
        <TextView
            android:id="@+id/text_sunday_evening"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Evening"
            app:layout_constraintBottom_toBottomOf="@id/checkbox_sunday_evening"
            app:layout_constraintStart_toEndOf="@id/checkbox_sunday_evening"
            app:layout_constraintTop_toTopOf="@id/checkbox_sunday_evening" />
        <!--for monday-->
        <CheckBox
            android:id="@+id/checkbox_monday_morning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:drawSelectorOnTop="true"
            app:layout_constraintStart_toStartOf="@id/checkbox_sunday_morning"
            app:layout_constraintTop_toBottomOf="@id/checkbox_sunday_morning" />
        
        <TextView
            android:id="@+id/text_monday_morning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="Morning"
            app:layout_constraintBottom_toBottomOf="@id/checkbox_monday_morning"
            app:layout_constraintStart_toEndOf="@id/checkbox_monday_morning"
            app:layout_constraintTop_toTopOf="@id/checkbox_monday_morning" />
        
        <CheckBox
            android:id="@+id/checkbox_monday_evening"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:drawSelectorOnTop="true"
            app:layout_constraintStart_toStartOf="@id/checkbox_sunday_evening"
            app:layout_constraintTop_toBottomOf="@id/checkbox_sunday_evening" />
        
        <TextView
            android:id="@+id/text_Monday_evening"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Evening"
            app:layout_constraintBottom_toBottomOf="@id/checkbox_monday_evening"
            app:layout_constraintStart_toEndOf="@id/checkbox_monday_evening"
            app:layout_constraintTop_toTopOf="@id/checkbox_monday_evening" />
        
        <TextView
            android:id="@+id/text_monday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:text="Monday:"
            app:layout_constraintBottom_toBottomOf="@id/checkbox_monday_morning"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/checkbox_monday_morning" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-05
          • 1970-01-01
          • 2016-10-31
          • 1970-01-01
          相关资源
          最近更新 更多