伙计我正在失去理智,我希望我的主要活动显示带有按钮的用户界面,一旦我点击我希望片段打开的按钮,但我似乎无法做到正确,我知道我必须创建一个ViewGroup容器,所以我可以使用Transaction.add方法,但我不知道下一步做什么或放在哪里。请帮助,提前致谢。
Main.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Fragment;
import android.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.btnFragmentA);
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Fragment newFragment = new Fragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment1 , newFragment);
transaction.commit();
}
});
}
}
片段
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentA extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.a_fragment, container, false);
}
}
主XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.dane.fragmenttest.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment A"
android:id="@+id/btnFragmentA"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
片段XML
<?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"
android:weightSum="0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the fragment"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
答案 0 :(得分:0)
嗯,我认为你只需做两件事。
例如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.dane.fragmenttest.MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button" />
<Button
android:id="@+id/btnFragmentA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Fragment A" />
</RelativeLayout>
onClickListener()
例如:
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
FragmentA newFragment = new FragmentA();
getFragmentManager().beginTransaction()
.add(R.id.container , newFragment)
.commit();
}
});
我希望这会有所帮助。