2013-02-14 93 views
1

我正在設計我的第一個Android應用程序,我正在嘗試創建XML佈局。 (我有與Java大量經驗)使用嵌套佈局定義特定的xml佈局

基本上我想做的事:

http://i.imgur.com/ZgbvYsX.png

凡輪廓描述:

藍:基本視圖(在那裏我可以設置文字)

紅色:一個ButtonView(用於使用戶可以同步數據)

綠色:嵌套的RelativeLayout(我要去的地方增添了許多其他的東西)

更具體是什麼我想做的事情:

enter image description here

所以我的問題是:如何我可以設置不同的佈局嗎?我在設置藍色和紅色視圖時遇到了一些麻煩,因爲我希望紅色視圖佔用屏幕寬度的25%左右(藍色填充最後的75%)。

預先感謝您。

+1

是否要創建自定義佈局?因爲它看起來像操作欄。 但是,如果你想單獨做,你正在尋找android:layout_weight =「。25」 - 它使用了25%的空間。將layout_width設置爲0dp或者您的視圖可能沒有正確縮放 – Martin 2013-02-14 09:55:28

+1

'RelativeLayout'就是你想要的.. – user370305 2013-02-14 09:55:56

+0

那麼它的意圖實際上是使它像一個操作欄 - 所以如果實際上有一個更簡單的方法來做到這一點,然後手動設置意見,請讓我知道。 而我已經嘗試了加權屬性(以及其他許多屬性),但我似乎無法正確理解。 – NicoPanduro 2013-02-14 09:57:52

回答

0

使用layout_weight屬性可指定視圖所佔的屏幕空間的百分比。

喜歡這裏,對e.g:

<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" 
android:orientation="vertical" .........> 

    <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" 
    android:orientation="horizontal"> 

      <EditText android:layout_height="wrap_content" android:layout_width="0dip" 
      android:layout_weight="3"/> 

      <Button android:layout_height="wrap_content" android:layout_width="0dip" 
      android:layout_weight="1"/> 

    </LinearLayout> 

    //Other view 

</LinearLayout> 
+0

這很簡單,可以爲我做。我謝謝你,先生。 – NicoPanduro 2013-02-14 11:03:12

0

下面的XML使用,這是專門爲你。

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

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_weight="0.75"> 

     <TextView 
      android:id="@+id/headingTextView" 
      style="@android:style/TextAppearance.WindowTitle" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 

      android:text="Cafe Vigeos" 
      android:textColor="@android:color/white" 
      android:textSize="30sp" /> 
     </RelativeLayout> 
     <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_weight="0.25"> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/nested_relative_layout" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" > 
    </RelativeLayout> 

</LinearLayout> 
+0

儘管我很欣賞額外的努力,但我無法完全理解這一點。我無法讓按鈕只能執行25%的操作。 – NicoPanduro 2013-02-14 11:04:52