【问题标题】:Android checkbox and text box on top of the otherAndroid复选框和文本框在另一个之上
【发布时间】:2017-08-25 13:08:04
【问题描述】:

我有下面的FrameLayout,这个布局是用来给我的RecyclerView 做行的。在蚀刻行中,我有文本框和复选框。问题是复选框和文本框相互连接,文本顶部的复选框隐藏了文本框。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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="56dp"
android:focusable="false"
>

<Button
    android:id="@+id/undo_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_undo"
    android:textAllCaps="true"
    android:textColor="@android:color/white"
    android:layout_gravity="end|center_vertical"
    style="@style/Base.Widget.AppCompat.Button.Borderless"
    />

<!-- quick and dirty divider -->
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"
    android:layout_gravity="bottom"
    />

<TextView
    android:id="@+id/title_text_view"
    android:layout_width="323dp"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:allowUndo="true"
    android:elevation="0dp"
    android:gravity="center_vertical"
    android:textSize="16sp"
    android:textStyle="bold" />

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

</FrameLayout>

如果有人能帮我解决这个问题,我会很高兴。谢谢。

【问题讨论】:

  • 你能分享屏幕截图吗?
  • 复选框有一个内置的 android:text 元素。你不能用吗?
  • @Nabil 这次我用不了了。
  • @JaiminPrajapti 我添加了一个屏幕截图。
  • 你能展示你想要实现的设计吗?

标签: android xml android-layout user-interface


【解决方案1】:

试试看。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="horizontal">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/checkBox" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World"/>
    </LinearLayout>
</RelativeLayout>

【讨论】:

    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:background="@color/greenbar"
            android:layout_height="wrap_content"
            android:focusable="false"
            >
    
            <Button
                android:id="@+id/undo_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button"
                android:textAllCaps="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:textColor="@android:color/white"
                android:layout_gravity="end|center_vertical"
                />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@android:color/darker_gray"
                android:layout_gravity="bottom"
                android:id="@+id/d"
                />
    
            <CheckBox
                android:id="@+id/dummycheckbox"
                android:layout_width="wrap_content"
                android:layout_below="@+id/d"
                android:layout_marginTop="10dp"
                android:layout_height="wrap_content"/>
    
            <TextView
                android:id="@+id/dummytittletxt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:layout_marginLeft="10dp"
                android:paddingTop="5dp"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/undo_button"
                android:layout_alignBottom="@+id/dummycheckbox"
                android:layout_toRightOf="@+id/dummycheckbox"
                android:gravity="center_vertical"
                android:textStyle="bold"
                android:text="textview" />
    
        </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      试试下面的布局:

      <?xml version="1.0" encoding="utf-8"?>
      <FrameLayout
      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="56dp"
      android:focusable="false"
      >
      
      <Button
          android:id="@+id/undo_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="button_undo"
          android:textAllCaps="true"
          android:textColor="@android:color/white"
          android:layout_gravity="end|center_vertical"
          style=""
          />
      
      <!-- quick and dirty divider -->
      <View
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="@android:color/darker_gray"
          android:layout_gravity="bottom"
          />
      
      <LinearLayout 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
      
          <CheckBox
          android:id="@+id/CheckBox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
      
      <TextView
          android:id="@+id/title_text_view"
          android:layout_width="323dp"
          android:layout_height="match_parent"
          android:allowUndo="true"
          android:elevation="0dp"
          android:gravity="center_vertical"
          android:textSize="16sp"
          android:textStyle="bold"
          android:text="textview" />
      
      
      </LinearLayout>
      </FrameLayout>
      

      【讨论】:

      • 文本在复选框的顶部,我需要使文本与复选框平行。还有其他选择吗?
      • @asaproG 尝试更新代码并根据需要更改 android:orientation="vertical"
      【解决方案4】:

      试试

      <?xml version="1.0" encoding="utf-8"?>
      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="56dp"
      android:focusable="false">
      
      <!-- quick and dirty divider -->
      <View
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:layout_gravity="bottom"
          android:background="@android:color/darker_gray" />
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="left|center_vertical"
          android:orientation="horizontal">
      
          <CheckBox
              android:id="@+id/CheckBox"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="0.1"
              android:checked="true" />
      
          <TextView
              android:id="@+id/title_text_view"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_gravity="center_vertical"
              android:layout_marginLeft="20dp"
              android:layout_weight="0.9"
              android:allowUndo="true"
              android:elevation="0dp"
              android:gravity="center_vertical"
              android:text="Aaadfdsaf"
              android:textColor="@android:color/black"
              android:textSize="16sp"
              android:textStyle="bold" />
      
      </LinearLayout>
      
      <Button
          android:id="@+id/undo_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="end|center_vertical"
          android:text="@string/button_undo"
          android:textAllCaps="true"
          android:textColor="@android:color/white"
          tools:text="Button" />
      

      改变你想要的重力和位置,我没有设计所以不能按照你的需要来做

      【讨论】:

        猜你喜欢
        • 2011-05-01
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多