我正在尝试从活动开始一个片段,但它不起作用,有人可以帮忙吗?
这是 main.java
public class MainActivity extends FragmentActivity {
Button clickButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickButton = (Button)findViewById(R.id.button1);
click();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void click()
{
clickButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("click","clickt");
FragmentOne fragmentOne = new FragmentOne();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentcontainer, fragmentOne);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
这是主要布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
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.example.testfragment.MainActivity" >
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:id="@+id/fragmentcontainer">
</FrameLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="57dp"
android:text="Button" />
</RelativeLayout>
这是片段
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.signup_, container,false);
}
}
这是片段布局
<?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="TextView" />
</LinearLayout>
答案 0 :(得分:1)
改变这个:
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:id="@+id/fragmentcontainer">
</FrameLayout>
对此:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentcontainer">
</FrameLayout>
解释:权重属性是LinearLayout
的布局参数,而不是RelativeLayout
的布局参数。布局参数被引用到View
/ ViewGroup
的容器(在这种情况下,View
/ ViewGroup
是您的FrameLayout
)。所以基本上这里的权重被完全忽略,宽度设置为0,使得FrameLayout
Fragment
次视图实际上不可见
答案 1 :(得分:0)
暂时为main.xml尝试此布局,并让我知道结果
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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.example.testfragment.MainActivity" >
<FrameLayout
android:id="@+id/fragmentcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" >
</FrameLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>