2012-10-31 62 views
0

我想創建一個在頁面底部有5個按鈕的應用程序,當用戶在任何按鈕上點擊 時,頁面將會改變,但5個按鈕仍然在頁面底部。現在我創建了 這個程序,但是當我在其他類中調用setContentView()時,我調用其他佈局, 按鈕被刪除。有剩餘5個按鈕的方法嗎?或者我應該在5layout中再次創建5個按鈕?有沒有辦法在所有頁面中保留之前的佈局?

感謝 乾杯

+0

搜索基本活動,爲所有類創建一個comman佈局,並將它們包含在您的課堂中... – Aamirkhan

回答

0

據我所知,我認爲你正在尋找的標籤,而不是按鈕。看看this

1

最簡單,最簡單的方法是製造具有佈局5個按鈕,並在所有的佈局文件 例

<include layout="@layout/titlebar"/> 

或者另一種方式是使用片段,THT的碎片與變化保持其他佈局的東西一樣
編輯
OR使用<merge>標籤
ŧ他<merge />標籤有助於消除視圖層次結構中包含一個佈局的多餘視圖組。例如,如果您的主佈局是一個垂直LinearLayout,其中兩個連續視圖可以在多個佈局中重新使用,那麼放置兩個視圖的可重用佈局需要它自己的根視圖。但是,使用另一個LinearLayout作爲可重用佈局的根將導致垂直LinearLayout內的垂直LinearLayout。除了減慢UI性能之外,嵌套的LinearLayout沒有真正的用途。

要避免包含這樣一個冗餘視圖組,您可以改爲使用該元素作爲可重用佈局的根視圖。例如:

<merge xmlns:android="http://schemas.android.com/apk/res/android"> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/add"/> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/delete"/> 

</merge> 

現在,當您在另一個佈局這個佈局(使用標籤),系統會忽略元素和佈局兩個按鈕直接放置,以代替標籤。

請通過this鏈接以獲得有關重用佈局

0

謝謝回答更多的信息。我閱讀開發人員鏈接,並添加合併,並將標籤包含到XML文件,但該程序有錯誤。爲什麼?

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<include layout="@layout/location"/> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<WebView 
    android:id="@+id/web_view" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1.0" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/background" 
    android:gravity="bottom" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button_home" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableTop="@drawable/home_icon" 
     android:text="@string/button_home" 
     android:textColor="@color/text_home" /> 

    <Button 
     android:id="@+id/button_product" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableTop="@drawable/product_icon" 
     android:onClick="Product" 
     android:text="@string/button_product" 
     android:textColor="@color/text_product" /> 

    <Button 
     android:id="@+id/button_places" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableTop="@drawable/places_icon" 
     android:onClick="Places" 
     android:text="@string/button_places" 
     android:textColor="@color/text_places" /> 

    <Button 
     android:id="@+id/button_rewards" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableTop="@drawable/rewards_icon" 
     android:onClick="Rewards" 
     android:text="@string/button_rewards" 
     android:textColor="@color/text_rewards" /> 

    <Button 
     android:id="@+id/button_more" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawableTop="@drawable/more_icon" 
     android:onClick="More" 
     android:text="@string/button_more" 
     android:textColor="@color/text_more" /> 
</LinearLayout> 

</LinearLayout> 

location.xml

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android"> 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
<TextView> 
<com.google.android.maps.MapView 
    android:id="@+id/mapView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:apiKey="0cPRv243zM1_S3ydsNg8MJP9_6BfCp642jOhPvQ"/> 
<LinearLayout 
    android:id="@+id/zoom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"/> 

</LinearLayout> 
</merge> 

什麼是不正確的? 謝謝

相關問題