更改 recyclerView 中的可见性 children

Changing visibility in recyclerView children

这里有 android 开发人员的小问题。正如第二张图片所示,我想在 EditText 附近有一个圆形(完美的圆形)按钮。为了实现这一点,我使用了一个单独的布局(第一张图片),我在其中定义了具有形状的按钮(如下所示)。因为我想要一个完美的圆,我把这个布局放在一个约束布局中,以保持比例。 现在,由于这种方式行不通:我怎样才能实现完美的圆圈? 考虑到这一点,我不能放入形状布局,因为此按钮的尺寸会因不同的活动而改变。

这是shape文件,用android:shape="oval"设置(ring不正常):

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid
    android:color="#666666"/>

这是按钮布局:

<?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:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<Button
    android:id="@+id/circle_button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="dadada"
    android:background="@drawable/ring_shape" />

</LinearLayout>

这是使用按钮的布局。考虑到这是一个 recyclerView 的单行:

<?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:id="@+id/category_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="6">

<android.support.constraint.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    <include
        layout="@layout/circle_button_layout"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintDimensionRatio="h,1:1" />

</android.support.constraint.ConstraintLayout>

<EditText
    android:id="@+id/category_name_row"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:inputType="textAutoCorrect"
    android:background="@android:color/transparent"
    android:layout_weight="3.4"
    android:textColor="@color/category_text"
    android:textSize="@dimen/payment_text"/>

<ImageButton
    android:id="@+id/category_btn_confirm_change"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_btn_done"
    android:tint="@color/hint_text"
    android:layout_gravity="center_vertical"
    android:background="@color/transparent"
    android:layout_marginRight="15dp"
    android:layout_marginEnd="15dp"
    android:layout_weight="0.8"/>

<ImageButton
    android:id="@+id/category_btn_delete"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_btn_delete"
    android:tint="@color/hint_text"
    android:layout_gravity="center_vertical"
    android:background="@color/transparent"
    android:layout_marginRight="15dp"
    android:layout_marginEnd="15dp"
    android:layout_weight="0.8"/>

 </LinearLayout>

一些图片只是为了清楚:

Single button layout

Row layout

您可以使用正方形布局。然后在里面把那个形状放在行布局中。 Use this square layout from github

在形状文件中试试这个。

<size
    android:width="36dp"
    android:height="36dp"/>

<solid android:color="#666666" />

您需要在布局中更改两个小东西:

  1. 去掉 LinearLayout 包装你的 Button:

    <?xml version="1.0" encoding="utf-8"?>
    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/circle_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="dadada"
        android:background="@drawable/ring_shape" />
    
  2. android:layout_width="0dp"android:layout_height="0dp" 添加到您的 <include/> 元素:

    <include
        android:layout_width="0dp"
        android:layout_height="0dp"
        layout="@layout/circle_button_layout"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintDimensionRatio="h,1:1" />