2012-04-10 127 views
0

我需要您的建議,關於我的設計,如果您有更好的想法(或者您同意我的觀點),那就太好了。出於某種原因,我有一種感覺,這是一個「石器時代編程風格」,但讓我們看看。可見和不可見的佈局

基本上我有我的xml相對佈局。在中心(垂直和horizenatlly)。我想顯示「或者」3個按鈕或3個文本取決於某些用戶輸入。所以我做了如下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clipChildren="false" 
    android:clipToPadding="false" > 

    <RelativeLayout android:id="@+id/Buttons" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" > 
     <Button1 .../> 
     <Button2 .../> 
     <Button3 .../> 
    </RelativeLayout> 

    <RelativeLayout android:id="@+id/Texts" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" > 
     <TextView1 .../> 
     <TextView2.../> 
     <TextView3.../> 
    </RelativeLayout> 

</RelativeLayout> 

根據用戶輸入的代碼,我設置能見度可見或不可見

難道這好嗎?如果不是,你建議什麼?

回答

2

在我看來,你做了什麼可能是原始的,但它很簡單,這是很好:)但是,如果你還是想一些選項,讓生活變得複雜,那麼

  1. 將每個塊的在單獨的xml中並使用include
  2. 製作2個視圖,然後使用ViewFlipper根據用戶需求進行翻轉。

但是,對於你的簡單要求,我認爲你做得很好。

include選項將工作這樣的事情,

layout_1.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/Buttons" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" > 
     <Button1 .../> 
     <Button2 .../> 
     <Button3 .../> 
</RelativeLayout> 

layout_2.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/Texts" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" > 
     <TextView1 .../> 
     <TextView2.../> 
     <TextView3.../> 
</RelativeLayout> 

您main_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clipChildren="false" 
    android:clipToPadding="false" > 

    <include layout="@layout/layout_1" /> 
    <include layout="@layout/layout_2" /> 

</RelativeLayout> 

include可以幫助您製作可重複使用的佈局,並且還可以幫助您保持XML文件不成比例,特別是當您有複雜的UI時。

+0

完美。我喜歡看到其他選項,以及我正在尋找的內容。如何將它放入2 diffenret xmls for futures maintaince.Can您請詳細說明「包含」?我知道如何使用鰭狀肢,但從未在 – Snake 2012-04-10 02:25:44

+0

之前看過包含,我正在使用include選項編輯答案。 – Shubhayu 2012-04-10 04:45:47

+0

太棒了。非常感謝,多數民衆贊成我正在尋找 – Snake 2012-04-10 16:24:39

3

你需要的是View.GONEView.VISIBLE

隨着View.GONE - 佈局不佔用任何空間(幾乎就好像它根本不存在)。如果使用View.INVISIBLE,對用戶來說,視圖(按鈕或案例中的文本)將不可見,但它們仍然會在屏幕上,從而將其他視圖(按鈕或文本)向上移動或下來(視圖不會死在中心)。

提示:您可以使用'android:layout_centerInParent'而不是'android:layout_centerHorizo​​ntal'和'android:layout_centerVertical'。

+0

謝謝你的答案+1 – Snake 2012-04-10 02:23:09

0

第一個選項是:

您可以添加所有的三個按鈕或textviews在LinearLayout中,通過使用android:layout_centerInParent居中父。

第二個選項是:

可以居中中間的按鈕了所有的三個按鈕,並與相應的其他兩個按鈕調整到中間按鈕。同樣,我們也應該對textviews重複這一點。在這個選項中,我們應該明確地將所有三個視圖顯示爲View.GONE

+0

第二個選項在Android透視圖視圖進行了優化,因爲我們減少了視圖數量。 – Pavandroid 2012-04-10 01:56:47

+0

感謝您的回答,儘管我認爲您關注的是中心視圖的一致性,而我的問題主要是如何在已存在於中心的2個視圖之間切換 – Snake 2012-04-10 02:24:38