在布局中制作 "settings-like" 个按钮
Make "settings-like" buttons in layouts
我是 Android 开发新手,恳请帮助。
我想在我的应用程序中制作 "settings-like"(全角、平面、带图像、带分隔符)按钮。这是示例:
Example image from Samsung settings app
我知道 "Preference" 观点 - 这不是解决方案。这种类型的按钮是否有任何原生视图?如果没有,使这个按钮成为可能的最合适的方法是什么?
更新:我已经成功地使用 LinearLayout 自己制作了按钮。您可以在下面的答案中找到我的实现。
您可以使用 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 class, ViewHolder 和 xml。
好的,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"
我是 Android 开发新手,恳请帮助。
我想在我的应用程序中制作 "settings-like"(全角、平面、带图像、带分隔符)按钮。这是示例: Example image from Samsung settings app
我知道 "Preference" 观点 - 这不是解决方案。这种类型的按钮是否有任何原生视图?如果没有,使这个按钮成为可能的最合适的方法是什么?
更新:我已经成功地使用 LinearLayout 自己制作了按钮。您可以在下面的答案中找到我的实现。
您可以使用 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 class, ViewHolder 和 xml。
好的,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"