2011-10-21 174 views
12

我已使用layout_weight参数将按钮的宽度设置为总布局宽度的70%,但似乎我错过了一些重要的细节以使其工作。使用重量的按钮宽度的Android相对布局

(另一种解决办法是用编程方式来display.getWidth()工作,但它也不管用,因为我不知道我的.xml应该是什么样子如果我选择设定button.setWidth()宽度)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:layout_weight="1.0"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="15px" 
     android:id="@+id/userVersionTextViewNew" 
     android:gravity="center" 
     android:layout_centerVertical="true"/> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="15px" 
     android:gravity="center" 
     android:layout_above="@id/userVersionTextViewNew" 
     android:id="@+id/userSoftSerialNumberTextView"/> 
    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/logo_200" 
     android:layout_above="@id/userSoftSerialNumberTextView" 
     android:layout_centerHorizontal="true"/>  
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="15px" 
     android:gravity="center" 
     android:layout_below="@id/userVersionTextViewNew" 
     android:id="@+id/dummyTextView"/>  
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/loginButton" 
     android:text="Σύνδεση" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/dummyTextView" 
     android:layout_weight="0.7"/> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/demoLoginButton" 
     android:text="Δοκιμαστική χρήση" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/loginButton" 
     android:layout_weight="0.7"/> 
</RelativeLayout> 
+0

看起来像这样可以帮助你:http://stackoverflow.com/questions/4961355/percentage-width-in-a-relativelayout – birdy

+0

在一面注意它不建议使用px与文本大小,而不是使用sp – Ahmed

+0

@艾哈迈德谢谢你。我会牢记这一点! :) – iCantSeeSharp

回答

12

试试这个..

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="15px" 
     android:id="@+id/userVersionTextViewNew" 
     android:gravity="center" 
     android:layout_centerVertical="true"/> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="15px" 
     android:gravity="center" 
     android:layout_above="@id/userVersionTextViewNew" 
     android:id="@+id/userSoftSerialNumberTextView"/> 
    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/logo_200" 
     android:layout_above="@id/userSoftSerialNumberTextView" 
     android:layout_centerHorizontal="true"/>  
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="15px" 
     android:gravity="center" 
     android:layout_below="@id/userVersionTextViewNew" 
     android:id="@+id/dummyTextView"/>  
    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:gravity = "center_horizontal" 
     android:layout_below="@id/dummyTextView" 
     android:id="@+id/loginButtonLayout" 
     android:weightSum="1.0"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/loginButton" 
      android:text="Σύνδεση" 
      android:layout_weight="0.7"/> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:gravity = "center_horizontal" 
     android:layout_below="@id/loginButtonLayout" 
     android:weightSum="1.0"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/demoLoginButton" 
      android:text="Δοκιμαστική χρήση" 
      android:layout_weight="0.7"/> 
    </LinearLayout> 
</RelativeLayout> 
+0

它的工作原理几乎完美,第一个按钮比第二个按钮稍小。我怀疑这是重要文本的大小。 – iCantSeeSharp

+1

为了得到两个完全相同大小的按钮,即屏幕宽度的70%,只需将按钮的宽度参数更改为 android:layout_width =“0dp” – chetanmoswal

+2

虽然此答案有效,但代码无法解释其本身。请参阅下面的更全面的答案,包括代码和对其原因的全面解释。 –

0

我想你不应该在相对布局标签定义android:layout_weight="1.0",如果要设置其他按钮的长度,则"wrap_content"

+0

不,它不会工作.. – iCantSeeSharp

+0

android:layout_weight只适用于LinearLayout,不适用RelativeLayout – Thupten

1

layout_weight,工作在的LinearLayout作为父。所以我认为问题在于此。你必须使用所有线性布局和相对布局的组合来实现你所需要的。

-2

首先,将参数android:weightSum="1.0"添加到容器(在这种情况下,将其添加到RelativeLayout)。

然后,定义他们每个孩子的体重。例如,如果你添加到一个按钮,这个按钮将占用总宽度的50%。

+0

它不会接受android:layout_width =“0”,它是“0px”,但按钮已经消失。 – iCantSeeSharp

+0

eclipse中的xml编辑器标记错误。 – iCantSeeSharp

+0

对不起,它是“0dp”或“0px”或类似...你必须在所有的孩子中设置这个参数 – vicentazo

-1

试试这个,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:layout_weight="10"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:textSize="15px" 
     android:id="@+id/userVersionTextViewNew" 
     android:layout_weight="0.75" 
    /> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:textSize="15px" 
     android:layout_weight="0.75" 
     android:id="@+id/userSoftSerialNumberTextView"/> 
    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:src="@drawable/logo_200" 
     android:layout_weight="0.75"/>  
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:textSize="15px" 
     android:layout_weight="0.75" 
     android:id="@+id/dummyTextView"/>   
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/loginButton" 
     android:text="Σύνδεση" 
     android:layout_weight="3.5"/> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/demoLoginButton" 
     android:text="Δοκιμαστική χρήση" 
     android:layout_weight="3.5"/> 
</LinearLayout> 
+0

我想你的意思是7.5而不是0.75。 – iCantSeeSharp

+0

不,我的意思是0.75 ...你想要70%的按钮然后..这将给70%的最后2个按钮和休息30%以上4文字和图像的观点..只是试图告诉我.. – MKJParekh

+0

仍然,这些按钮的宽度为100%,而且它们占据了总窗口的一半,超过了标签内容的50%。 – iCantSeeSharp

6

我不认为一个RelativeLayoutlayout_weight作品。也许你应该在RelativeLayout里面添加一个LinearLayout,并在里面使用layout_weight

而且使用layout_weight时,你通常必须有宽度或定义为0dp对象的高度,使你的情况是这样的:

android:layout_weight="0.7" 
android:layout_height="0dp" 
30

问题

您不能在RelativeLa上使用layout_weight参数YOUT。这些是来自LinearLayout的参数。我会在下面提供更多关于差异的信息。但首先关于这个问题

一个解决方案

使用LinearLayout中,您可以在一个行位置元素具有重量分布的解决方案。 不要忘记添加layout_weights时使用0dp宽度!以下示例显示70/30的重量分布。

<LinearLayout 
android:id="@+id/wrapper" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:weightSum="1.0" > 

    <Button 
     android:text="left" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".70" /> 

    <Button 
     android:text="right" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".30" /> 

</LinearLayout> 

所有这一切都在您的代码中已经有的RelativeLayout中。这个答案的其余部分是背景信息,每个有这些问题的人都应该阅读以了解他们在做什么。

RelativeLayout的

无论何时启动与多个元素的布局我劝你喜欢的RelativeLayout赞成线性事情。 RelativeLayout非常强大,可以让你将元素相互关联(leftOf,below,...)。在大多数情况下,这比你所需要的要多得多。

the android development document一个例子(相信我,这一切都没有):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" > 
    <EditText 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/reminder" /> 
    <Spinner 
     android:id="@+id/dates" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/name" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/times" /> 
    <Spinner 
     android:id="@id/times" 
     android:layout_width="96dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/name" 
     android:layout_alignParentRight="true" /> 
    <Button 
     android:layout_width="96dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/times" 
     android:layout_alignParentRight="true" 
     android:text="@string/done" /> 
</RelativeLayout> 

的LinearLayout

的LinearLayout中看起来很能干的太多,但为了得到一切排序,只有线性电源,你会很可能开始嵌套这些布局。这就是明智的丑陋表现。

再次来自the android development documentation的示例。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:orientation="vertical" > 
    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/to" /> 
    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/subject" /> 
    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:gravity="top" 
     android:hint="@string/message" /> 
    <Button 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:text="@string/send" /> 
</LinearLayout> 
6

我知道这个问题是旧的,只是有人谁寻找一个解决方案:

谷歌推出了名为android.support.percent

PercentRelativeLayout正是你的情况下,新的API:

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"> 

    <!-- Other controls --> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/loginButton" 
     android:text="Σύνδεση" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/dummyTextView" 
     app:layout_widthPercent="70%"/> 

    <!-- Other controls --> 

</android.support.percent.PercentRelativeLayout> 
0

正如@ hcpl在他的回答中正确提到的那样:

您不能在RelativeLayout上使用layout_weight参数。这些是来自LinearLayout的参数。

是的,他是对的!但请考虑由嵌套布局造成的negative impact on performance

随着ConstraintLayout的引入,您可以在不嵌套LinearLayout的情况下解决您的问题。您只需粘贴两条垂直指南,边距为15%和85%,然后在两者之间放置按钮。

Layout Editor

这里的布局源代码:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"> 

    <TextView 
     android:id="@+id/userVersionTextViewNew" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="15sp" 
     android:text="userVersionTextViewNew" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toTopOf="@+id/userSoftSerialNumberTextView" 
     app:layout_constraintVertical_chainStyle="packed" /> 

    <TextView 
     android:id="@+id/userSoftSerialNumberTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="15sp" 
     android:text="userSoftSerialNumberTextView" 
     app:layout_constraintTop_toBottomOf="@+id/userVersionTextViewNew" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toTopOf="@+id/imageView" /> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/logo_200" 
     app:layout_constraintTop_toBottomOf="@+id/userSoftSerialNumberTextView" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toTopOf="@+id/dummyTextView" /> 

    <TextView 
     android:id="@+id/dummyTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="15sp" 
     android:text="dummyTextView" 
     app:layout_constraintTop_toBottomOf="@+id/imageView" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toTopOf="@+id/loginButton" /> 

    <Button 
     android:id="@+id/loginButton" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Σύνδεση" 
     app:layout_constraintTop_toBottomOf="@+id/dummyTextView" 
     app:layout_constraintLeft_toLeftOf="@+id/leftGuideline" 
     app:layout_constraintRight_toLeftOf="@+id/rightGuideline" 
     app:layout_constraintBottom_toTopOf="@+id/demoLoginButton" /> 

    <Button 
     android:id="@+id/demoLoginButton" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Δοκιμαστική χρήση" 
     app:layout_constraintTop_toBottomOf="@+id/loginButton" 
     app:layout_constraintLeft_toLeftOf="@+id/leftGuideline" 
     app:layout_constraintRight_toLeftOf="@+id/rightGuideline" 
     app:layout_constraintBottom_toBottomOf="parent" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/leftGuideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.15" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/rightGuideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.85" /> 

</android.support.constraint.ConstraintLayout> 

结果你得到这样的观点:

Result view

可以在Building interfaces with ConstraintLayout了解详情。