【问题标题】:Make "settings-like" buttons in layouts在布局中制作“类似设置”的按钮
【发布时间】:2018-02-14 09:38:13
【问题描述】:

我是 Android 开发新手,恳请帮助。

我想在我的应用程序中制作“类似设置”(全角、扁平、带图像、带分隔符)按钮。这是示例: Example image from Samsung settings app

我知道“偏好”视图 - 这不是解决方案。这种类型的按钮是否有任何本机视图?如果没有,使这个按钮成为可能的最合适的方法是什么?

更新:我已经设法使用 LinearLayout 自己制作了按钮。您可以在下面的答案中找到我的实现。

【问题讨论】:

  • 使用ListView
  • 这种类型的按钮没有视图。您应该使用标准视图自行设计。这很容易做到
  • edit您的问题显示the code you have so far。您应该至少包含您遇到问题的代码的大纲(但最好是minimal reproducible example),然后我们可以尝试帮助解决具体问题。您还应该阅读How to Ask

标签: android android-layout android-button


【解决方案1】:

您可以使用RecyclerView。下面是xml的例子:

<LinearLayout

    ...>

    <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:id="@+id/recycler"
            android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

在 Java 中:

RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
            linearLayoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);

recyclerView.setAdapter(new RecyclerAdapter(this, /*list of your items*/));

您必须为列表中的一项创建自己的RecyclerAdapter 类、ViewHolder 和 xml。

【讨论】:

    【解决方案2】:

    好的,Android 中似乎没有此类按钮的原生视图。

    RecyclerView 和 ListView 无济于事,因为它们需要适配器(我不需要适配器,我只需要简单的按钮)。

    所以,我决定使用线性布局来制作这些按钮,代码如下:

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:orientation="horizontal"
        android:padding="16dp">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_sim_card" />
    
        <Space
            android:layout_width="16dp"
            android:layout_height="wrap_content" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/action_edit" />
    
    </LinearLayout>
    

    制作线性布局 android:clickable="true"android:focusable="true" (稍后你可以在代码中绑定 onClickListener )因为它会像一个按钮。重点是制作类似按钮的动画(点击时的墨水效果) - 指定背景属性: android:background="?android:attr/selectableItemBackground"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      相关资源
      最近更新 更多