点击按钮后,我的应用程序上的碎片不会出现



我是安卓开发游戏的新手,我研究并阅读了大量关于碎片的信息,但我似乎遇到了一些障碍。在创建了所有内容并检查以确保我没有错误后,我的应用程序没有按我想要的方式运行。点击按钮,我想在屏幕上显示一个片段。我有两个按钮,所以有两个碎片,但是我的按钮似乎不起作用。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="35dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/signin_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:gravity="center"
android:text="Sign In"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/signup_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:gravity="center"
android:text="Sign Up"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:id="@+id/parent_layout"
>
<FrameLayout
android:id="@+id/signin_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />

</LinearLayout>
</LinearLayout>

以下是java代码:

package com.example.projectconviencesplash;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class SigninMainActivity extends Fragment implements View.OnClickListener {
public SigninMainActivity() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.signinfragment, container, false);
Button signinBtn = (Button) rootView.findViewById(R.id.signin_button);
Button signupBtn = (Button) rootView.findViewById(R.id.signup_button);
signinBtn.setOnClickListener(this);
signupBtn.setOnClickListener(this);

return rootView;
}
@Override
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.signin_button:
fragment = new SigninTwoActivity();
replaceFragment(fragment);
break;
case R.id.signup_button:
fragment = new SignupTwoActivity();
replaceFragment(fragment);
break;
}
}
public void replaceFragment(Fragment someFragment) {
assert getFragmentManager() != null;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.signin_fragment, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}

}

以下是片段xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp">
<Button
android:id="@+id/dummy_button"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
android:layout_weight="0"
android:background="@drawable/fb_button"
android:drawableStart="@drawable/fb"
android:drawablePadding="-70dp"
android:paddingStart="70dp"
android:text="@string/fb_Title"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<Button
android:id="@+id/dummy_button"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
android:layout_weight="0"
android:background="@drawable/g_button"
android:drawableStart="@drawable/g"
android:drawablePadding="-80dp"
android:paddingStart="80dp"
android:text="@string/g_Title"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:src="@drawable/strike" />
<TextView
android:layout_width="149dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:text="@string/email_Title"
android:textColor="#cccccc"
android:textSize="16sp"
android:textStyle="" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:src="@drawable/strike" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:gravity="start"
android:text="@string/email"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1.5"
android:background="@drawable/edittextbackground"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:gravity="start"
android:text="@string/password"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1.5"
android:background="@drawable/edittextbackground"
android:inputType="text"/>
</LinearLayout>
<Button
android:id="@+id/dummy_button"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
android:layout_weight="0"
android:background="@drawable/rounded_corners"
android:drawablePadding="-10dp"
android:paddingStart="30dp"
android:text="@string/signin"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>

最后,这里是片段java:

package com.example.projectconviencesplash;
import androidx.fragment.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.FragmentManager;
public class SigninTwoActivity extends Fragment {
@Override
public View  onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.signinfragment, container, false);
}
}

同样,当我点击这些按钮时,它们似乎什么都不做。此外,如果有人觉得特别慷慨,我很想知道如何在加载此活动时将我的一个片段设为默认片段,这样在第一次加载时它就不仅仅是一个有两个按钮的空白屏幕。我很感激大家对我的帮助!

我将使用更基本的代码,因为它将更容易理解,并有望在其他项目中实现它时有所帮助。我在下面提供的代码将有一个MainActivity、FragmentOne、FragmentTwo和FragmentThree。MainActivity将在启动时加载FragmentOne,FragmentOne将有两个按钮,一个用于加载FragmentTwo,另一个用于装载FragmentThree。

activity_main.xml此FrameLayout将容纳碎片,请注意ID fragment_container。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

主要活动活动应扩展AppCompatActivity

public class MainActivity extends AppCompatActivity {
/**
* Called when the activity is created
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Reference to Fragment one object
FragmentOne fragOne = new FragmentOne();
// Reference to fragment transaction.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// Fragment Transaction
fragmentTransaction.add(R.id.fragment_container, fragOne)
.addToBackStack(null) // Adds fragment to back stack, for backward navigation.
.commit(); // Finishes the fragment transaction.
}
}

fragment_one.xml这里没什么特别的。

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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=".FragmentOne"
android:orientation="vertical">

<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment One"
android:textSize="32sp"
tools:layout_editor_absoluteX="143dp" />
<Button
android:id="@+id/btn_fragment_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment two"
tools:layout_editor_absoluteY="401dp" />
<Button
android:id="@+id/btn_fragment_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment three"
tools:layout_editor_absoluteY="401dp" />
</androidx.appcompat.widget.LinearLayoutCompat>

FragmentOne

public class FragmentOne extends Fragment {
/**
* Called when the fragment loads its user interface
*/
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one,container, false);
// Reference the fragments
final FragmentTwo fragTwo = new FragmentTwo();
final FragmentThree fragThree = new FragmentThree();
// Create FragmentTransaction, getActivity returns context of the activity in a fragment.
final FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
// Reference Button view.
Button loadFragTwoBtn = v.findViewById(R.id.btn_fragment_two);
// Set clickListener
loadFragTwoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction.replace(R.id.fragment_container, fragTwo)
.addToBackStack(null)
.commit();
}
});
Button loadFragThreeBtn = v.findViewById(R.id.btn_fragment_three);
loadFragThreeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction.replace(R.id.fragment_container, fragThree)
.addToBackStack(null)
.commit();
}
});
return v;
}
}

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentTwo">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment Two"
android:textSize="32sp"/>
</FrameLayout>

FragmentTwo

public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
return view;
}
}

fragment_three公共类FragmentTwo扩展Fragment{

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

}


FragmentThree

公共类FragmentThree扩展Fragment{

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

}

Hope this is helpful, I would also strongly recommend doing the fragment code labs to help build a strong foundation. 
Additional note, 
When providing an ID for a button the ID has to be unique, in your layout, all of your buttons use the same ID. 

如果要替换片段中的片段,请使用getChildFragmentManager而不是getFragmentManager

最新更新