2017-03-28 16 views
0

我想學習如何建立一個Android應用程序main_activity,我困惑於:顯示圖像choosen和改變第二次發射

1.首先啓動的時間我的應用程序將顯示viewpagers img_btn冰, img_btn果凍。當img_buttons點擊將顯示在profil.xml中選擇哪個圖像,有沒有解決辦法在1 xml或我必須爲每個按鈕創建2 xml?

2.如果用戶選擇了img_button,main_activity將更改爲profil.xml,因此第二次啓動時將顯示profil.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

<?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

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和page2.java與page1相同 英語不是我的第一語言,請原諒任何錯誤。

+0

可以顯示你的xlm。和代碼。你還想要什麼圖像?在viewpager上?或一些imageView? – kggoh

+0

研究共享的推理。 – Phil3992

+0

您可以將圖片網址傳遞到第二個活動,點擊按鈕後顯示您的圖片。 'Intent intent = new Intent(this,Activity2.class); String imageUrl =「把你的圖片網址放在這裏」; intent.putExtra(「url」,imageUrl); startActivity(intent);' –

回答

0

我不確定你擴展了什麼片段。導入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)和兩類延伸片段,則可以使用上述功能先添加的片段,隨後在打開新碎片時使用替換。注意函數中有一個R.id.content_frame。這是從你的main_activity.xlm中,將一個linear_layout命名爲content_frame,它將保存打開的片段。然後在page1.xml上放置一個按鈕,當頁面1上的按鈕被點擊時,將ImageView放入page2.xml中。使用replaceFragment打開page2.xml

相關問題