我想学习如何创建一个Android应用程序,我很困惑:
1.第一次启动我的应用程序将显示viewpagers img_btn ice,img_btn jelly。当单击img_buttons将显示在profil.xml中选择的图像时,是否有任何解决方案在1 xml中执行,或者我必须为每个按钮创建2 xml?
2.如果用户选择了img_button,main_activity将更改为profil.xml,因此第二次启动时将显示profil.xml。我怎么能这样做?
main_activity.xml
Save
page1.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#000000"
>
<TextView
android:text="Choose char"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#1e00ff"/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_ViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"/>
page1.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Halaman 2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="54dp"
android:textColor="#0aedff"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:background="#00000000"
android:src="@drawable/btn_info"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical|center"/>
page2.xml和page2.java与page1相同 英语不是我的第一语言,所以请原谅任何错误。
答案 0 :(得分:0)
我不确定你扩展了什么碎片。 import android.support.v4.app.Fragment;或者导入android.app.Fragment;以下示例假设您使用support.v4
public void replaceFragment(Fragment fragment, String title) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(title)
.commit();
}
public void addFragment(Fragment fragment, String title) {
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, fragment, title)
.addToBackStack(title)
.commit();
}
你最好有2个xml(例如page1.xml,page2.xml)和两个类扩展Fragment,你可以使用上面的函数先添加一个片段,然后在打开新片段时使用replace。请注意,给定的函数中有一个R.id.content_frame。这是来自你的main_activity.xlm,将linear_layout命名为content_frame,它将保存打开的片段。然后在page1.xml上放置一个按钮,在单击page1上的按钮时将pageView放在page2.xml中。使用replaceFragment打开page2.xml