2013-02-14 132 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