在屏幕中心的Fragment图层上添加一个可单击按钮

时间:2017-02-17 08:10:36

标签: java android html5

我想在屏幕中间的片段图层上添加按钮,添加按钮但不是点击&进入理想的活动。这是我的java和amp;的代码XML: - xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/a6">

    <Button
        android:id="@+id/hardware"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="213dp"
        android:background="@android:color/darker_gray"
        android:text="Button" />
</RelativeLayout> 

Java代码

public class SwapFragmentsix extends Fragment {
    Button hardware;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_swap_fragmentsix, container, false);
        hardware = (Button) view.findViewById(R.id.hardware);
        hardware.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HardwareKeyPoints.class);
                startActivity(intent);
            }
        });
        return inflater.inflate(R.layout.activity_swap_fragmentsix, container, false);
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

你应该试试这个

public class SwapFragmentsix extends Fragment {
        Button hardware;


        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.activity_swap_fragmentsix, container, false);
            hardware = (Button) view.findViewById(R.id.hardware);
            hardware.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), HardwareKeyPoints.class);
                    startActivity(intent);
                }
            });
            return view;  //change to this
        }
    }
  

因为你回来了   inflater.inflate(R.layout.activity_swap_fragmentsix, container,false);代替view。那就是你再次夸大观点。