[英]display image choosen in xml and change main_activity on second time launch
i want to learn how to create an android app, and i'm confused at : 我想学习如何创建一个android应用程序,我感到困惑:
1.first time launching my app will display viewpagers img_btn ice, img_btn jelly. 1.第一次启动我的应用程序时,将显示viewpagers img_btn ice,img_btn jelly。 and when img_buttons clicked will display which image chosen in profil.xml , is there any solution do it in 1 xml or i must create 2 xml for each button?
当单击img_buttons时 ,将显示在profil.xml中选择的图像,请问有没有解决的办法以1 xml或我必须为每个按钮创建2 xml?
2.if user have choosed img_button, main_activity will change to profil.xml, so for the second time launch will show profil.xml. 2.如果用户选择了img_button,main_activity将更改为profil.xml,因此第二次启动将显示profil.xml。 how can i do that?
我怎样才能做到这一点?
main_activity.xml main_activity.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.xml page1.xml
<?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"/>
page1.java page1.java
import android.support.v4.app.*;
import android.view.*;
import android.os.*;
public class page1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v=inflater.inflate(R.layout.halaman_1, container,false);
return v;
}
}
page2.xml and page2.java same as page1 English isn't my first language, so please excuse any mistakes. 与page1相同的page2.xml和page2.java英语不是我的母语,因此请原谅任何错误。
I am not sure what Fragment you extended. 我不确定您扩展了什么片段。 import android.support.v4.app.Fragment;
导入android.support.v4.app.Fragment; or import android.app.Fragment;
或导入android.app.Fragment; The below example assume you use support.v4
以下示例假定您使用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();
}
you better has 2 xml (eg. page1.xml, page2.xml) and two classes extend Fragment, you may use the above function to first add a fragment, subsequently use replace when you open new fragments. 您最好有2个xml(例如page1.xml,page2.xml)和两个扩展Fragment的类,您可以使用上面的函数先添加一个片段,然后在打开新片段时使用replace。 Notice there is a R.id.content_frame in the functions given.
注意,给定的函数中有一个R.id.content_frame。 That is from your main_activity.xlm, make a linear_layout name it content_frame, which will hold the open fragment.
那是从您的main_activity.xlm中,将一个linear_layout名称命名为content_frame,它将保存打开的片段。 You then put a button on page1.xml, put an ImageView inside page2.xml, when the button on page1 has been clicked.
然后,当单击page1上的按钮时,将一个按钮放在page1.xml上,将ImageView放在page2.xml内。 use replaceFragment to open page2.xml
使用replaceFragment打开page2.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.