【问题标题】:Button in Relativelayout was stretched相对布局中的按钮被拉伸
【发布时间】:2011-03-02 22:19:40
【问题描述】:

基本上我想在这个相对布局中添加一个 textView 、微调器和一个按钮。看起来像:

---- 文本视图----

-----微调器----

------------按钮--

问题是右下角的按钮被拉伸了,按钮高度代表了所有的空间。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/surveyLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:layout_alignParentTop="true"
    android:text="@string/selectSurvey"
    android:textColor="@android:color/darker_gray"
    android:id="@+id/hasSurveyLabel"
    />
<Spinner
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/surveySpinner"
    android:layout_below="@+id/hasSurveyLabel"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:id="@+id/startButton"
    android:text="@string/start"
    **android:layout_below="@+id/surveySpinner"**
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:paddingTop="10dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    />

看起来是因为layout_below,当我们使用layout_below时,spinner下方的按钮,它必须在它的正下方,除非你定义它,否则没有空格。
我们如何解决这个问题?
我所期待的是,将按钮放在微调器下方和屏幕底部。
我已经为此苦苦挣扎了几天,有什么帮助吗?谢谢。

【问题讨论】:

    标签: android android-relativelayout stretched


    【解决方案1】:

    我会删除对微调器的引用并将其放在右下角。我有类似的屏幕布局,以下按钮设置适合我。

    <Button
       android:id  ="@+id/testButton"
       android:text="Testing"
       android:layout_width ="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_alignParentRight ="true"  /> 
    

    或者,我所看到的是将它包装在放置在底部的 LinearLayout 中。但对我来说,分层似乎不需要。希望这会有所帮助!

       <LinearLayout
          android:orientation  ="horizontal"
          android:layout_width ="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight ="true">
    
          <Button
             android:id  ="@+id/testButton"
             android:text="Testing"
             android:layout_width ="wrap_content"
             android:layout_height="wrap_content"  />
    
       </LinearLayout>
    

    【讨论】:

    • 我之前也尝试过第一个解决方案,但是当你改变方向时,按钮会被裁剪。
    【解决方案2】:

    只有高度被拉伸了吗?如果是这样,我会删除 android:layout_alignParentBottom="true" 并只让它引用 Spinner 并将其与右侧对齐...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多