将布局替换为 Android 中的另一个布局
Replacing layout with another in Android
我有一个应用程序,用户可以通过按一个按钮来更改布局的内容,但是当我想替换布局时,我在替换行中遇到错误:
这是addtotry.xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="150px"
android:layout_height="fill_parent"
android:background="#e7e7e7"
android:id="@+id/linearLayout">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="adrese"
android:id="@+id/adreseButton"
android:layout_below="@+id/infoButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="350px"
android:layout_toRightOf="@+id/linearLayout"
android:layout_toEndOf="@+id/linearLayout"
android:id="@+id/imageLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="350px"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/imageview"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="@+id/linearLayout"
android:layout_toEndOf="@+id/linearLayout"
android:layout_below="@+id/imageLayout"
android:id="@+id/fragment_container">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text"
android:id="@+id/textView7"/>
</RelativeLayout>
</RelativeLayout>
这是MainActivity
:
public class MainActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addtotry);
Button btnLoad = (Button) findViewById(R.id.adreseButton);
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentVieta fh = new FragmentVieta();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, fh);
ft.commit();
}
});
}
}
我现在不知道哪里出错了,因为我创建了 FragmentView 并且它扩展了片段。您知道如何解决图片中显示的问题吗?因为我改为 (int, com.wunderlist.slidinglayersample) 并且它说需要表达式。
检查您的 FragmentVieta
是否扩展 android.app.Fragment 或支持一个?
当您使用 getFragmentManager()
时,FragmentVieta
必须仅从 android.app.Fragment
扩展。
我有一个应用程序,用户可以通过按一个按钮来更改布局的内容,但是当我想替换布局时,我在替换行中遇到错误:
这是addtotry.xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="150px"
android:layout_height="fill_parent"
android:background="#e7e7e7"
android:id="@+id/linearLayout">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="adrese"
android:id="@+id/adreseButton"
android:layout_below="@+id/infoButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="350px"
android:layout_toRightOf="@+id/linearLayout"
android:layout_toEndOf="@+id/linearLayout"
android:id="@+id/imageLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="350px"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/imageview"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="@+id/linearLayout"
android:layout_toEndOf="@+id/linearLayout"
android:layout_below="@+id/imageLayout"
android:id="@+id/fragment_container">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text"
android:id="@+id/textView7"/>
</RelativeLayout>
</RelativeLayout>
这是MainActivity
:
public class MainActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addtotry);
Button btnLoad = (Button) findViewById(R.id.adreseButton);
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentVieta fh = new FragmentVieta();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, fh);
ft.commit();
}
});
}
}
我现在不知道哪里出错了,因为我创建了 FragmentView 并且它扩展了片段。您知道如何解决图片中显示的问题吗?因为我改为 (int, com.wunderlist.slidinglayersample) 并且它说需要表达式。
检查您的 FragmentVieta
是否扩展 android.app.Fragment 或支持一个?
当您使用 getFragmentManager()
时,FragmentVieta
必须仅从 android.app.Fragment
扩展。