我试图使我的imagebutton变成另一个活动,当我点击按钮,但我不知道我怎么能在一个片段页上这样做。
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AboutFragment">
<!-- TODO: Update blank fragment layout -->
<ImageButton
android:id="@+id/imageButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="top|right"
android:layout_marginTop="200dp"
android:src="@drawable/ic_launcher_foreground" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="About"
android:textSize="48sp"
android:textStyle="bold" />
</FrameLayout>
java代码:
package com.example.voiz;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
* Use the {@link AboutFragment#newInstance} factory method to
* create an instance of this fragment.
*
*/
public class AboutFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment AboutFragment.
*/
// TODO: Rename and change types and number of parameters
public static AboutFragment newInstance(String param1, String param2) {
AboutFragment fragment = new AboutFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public AboutFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
}
}
我是androidstudio的新手,目前使用的版本是4.1.3,我不太熟悉片段编码,因为我通常在活动中编写代码。我正在努力分配碎片Java,我也遵循这个教程https://www.youtube.com/watch?v=miphbotsywy&t=244s 为什么我的项目文件中有片段。提前谢谢你。
1条答案
按热度按时间2mbi3lxu1#
所以对于下面的代码片段可能有用。首先,你应该移除所有不必要的东西[1],然后改变膨胀碎片的方式[2]。对于你的imagebutton这样的调用[3]。然后在oncreateview中,您可以从xml文件中调用imagebutton,如下所示[4]。如果你有任何疑问,请告诉我。