2013-03-01 28 views
0

我想爲android平臺創建一個靈活的用戶界面。允許用戶添加/刪除按鈕和按鈕自動移位

基本上我的佈局將包含一個圖像按鈕,在開頭處帶有「加號」標記。 當用戶點擊它。在用戶提示命名想要創建的圖像按鈕時調用另一個佈局。當用戶單擊確認時,他將回到第一佈局,並且新添加的圖像按鈕將替換「加「標誌圖像按鈕,加號圖像按鈕將向右移動。

我知道如何創建佈局最初,但我不知道如何編程它以上述方式運作。

確定,所以現在我有我的主要佈局文件中設置是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" > 

<RelativeLayout 

    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#000000" > 
    <ImageButton 
     android:id="@+id/addRoom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="25dp" 
     android:layout_marginTop="38dp" 
     android:background="#80000000" 
     android:src="@drawable/plus" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="@+id/addRoom" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/addRoom" 
     android:layout_alignRight="@+id/addRoom" 
     android:layout_below="@+id/addRoom" 
     android:text="@string/add_room" 
     android:gravity="center" 
     android:textColor="#FFFFFF" /> 

    </RelativeLayout> 
</ScrollView> 

將我有任何問題,當我包括片段轉移圖像按鈕和TextView的當對準指示,兩者的它。

回答

2

當您想要由用戶實時操作您的活動的UI時,建議使用Fragments和Fragment Manager來更改活動視圖的不同部分。

您可以在文檔中大量信息:

http://developer.android.com/guide/components/fragments.html

http://marakana.com/s/post/1250/android_fragments_tutorial

你的情況

你會做一些事情這樣:

fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.remove(plusButtonFragment) 
.add(R.id.containerForFragments,userNewFragment) 
.add(R.id.containerForFragments, plusButtonFragment)).commit(); 

在創建完成後plusButtonFragmentuserNewFragment

更新:

對於在相對佈局管理片段位置檢查出的下一篇文章:

How to add fragments programmatically in relativelayout

+0

我理解你上面的示例代碼調用和替換圖像按鈕,但有沒有辦法設置擺放呢?我打算讓它每行2個圖像按鈕。我的xml佈局是相對佈局。 – user2124761 2013-03-02 09:12:07

+0

查看更新的答案。 – 2013-03-02 13:38:42

+0

我附上了我的佈局文件代碼 – user2124761 2013-03-02 15:18:40