微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 有没有简单的方法来创建一个Action Button片段?

操作按钮是这样的:

我认为框架应该提供易于使用的片段,其中开发者只提供标题,图标,背景图像和监听器.我没有在文档中找到它,你知道吗?

解决方法

我根据 design guidelines结束自己写的:

ActionFragment.java

public class ActionFragment extends Fragment implements View.OnClickListener {

    private static Listener mListener;
    private CircledImageView vIcon;
    private TextView vLabel;

    public static ActionFragment create(int iconResId,int labelResId,Listener listener) {
        mListener = listener;
        ActionFragment fragment = new ActionFragment();
        Bundle args = new Bundle();
        args.putInt("ICON",iconResId);
        args.putInt("LABEL",labelResId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_action,container,false);
    }

    @Override
    public void onViewCreated(View view,@Nullable Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        vIcon = (CircledImageView) view.findViewById(R.id.icon);
        vLabel = (TextView) view.findViewById(R.id.label);
        vIcon.setimageResource(getArguments().getInt("ICON"));
        vLabel.setText(getArguments().getInt("LABEL"));
        view.setonClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mListener.onActionPerformed();
    }

    public interface Listener {
        public void onActionPerformed();
    }
}

布局/ fragment_action.xml

<?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:orientation="vertical"
              android:paddingLeft="16dp"
              android:paddingRight="16dp"
              android:paddingTop="24dp"
              android:paddingBottom="12dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.wearable.view.CircledImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        tools:src="@drawable/ic_full_cancel"
        app:circle_color="@color/action_button"
        app:circle_radius="52dp"
        app:circle_border_width="0dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        tools:text="Cancel"
        android:id="@+id/label"
        android:fontFamily="sans-serif-condensed-light"
        android:textSize="20sp"
        android:textColor="@color/white"/>

</LinearLayout>

彩色/ action_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#2878ff" android:state_pressed="false" />
    <item android:color="#2955C5" android:state_pressed="true" />
</selector>

使用示例(例如FragmentGridPagerAdapter)

ActionFragment.create(R.drawable.ic_full_open_on_phone,R.string.open_on_phone,new ActionFragment.Listener() {
                    @Override
                    public void onActionPerformed() {

                    }
                });

原文地址:https://www.jb51.cc/android/311250.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐