android - 在相对布局中格式化按钮大小

标签 android button android-linearlayout android-relativelayout

我正在创建一个反馈表,最后有两个按钮,“清除”和“发送”。这必须在相对布局中完成。

问题是按钮不够宽,我希望按钮与其上方的宽度相匹配。

这是尝试更改宽度之前的应用程序:

enter image description here

和代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/feedback_name_hint" >
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:hint="@string/feedback_email_hint" >
    </EditText>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ratingBar1"
        android:layout_alignLeft="@+id/editText2"
        android:layout_alignRight="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:gravity="center_vertical|top"
        android:hint="@string/feedback_actual"
        android:inputType="textMultiLine" >

    </EditText>

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/Button01"
        android:layout_alignLeft="@+id/editText3"
        android:layout_marginBottom="21dp" />

  <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ratingBar1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="18dp"
        android:text="@string/Clear" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="@string/Send" /> 


</RelativeLayout>

我尝试在按钮周围创建一个线性布局,如下所示:

    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:orientation="horizontal" >

    <Button 
        android:id="@+id/button3"
        android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="@string/Clear"
    />
    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="@string/Send"
    />
 </LinearLayout>

但是,虽然按钮的大小是我想要的,但它们会跳到 View 的顶部,并影响其他元素:

enter image description here

如有任何帮助,我们将不胜感激,谢谢。

已解决:

它通过使用以下线性布局包围按钮来工作:(请注意评级栏是如何设置在 linearlayoutid 之上的)

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearlayout_id"
        android:layout_alignLeft="@+id/editText3"
        android:layout_marginBottom="21dp" />

 <LinearLayout 
     android:id="@+id/linearlayout_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal"
        android:layout_marginBottom="18dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/Clear" />

        <Button
            android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/Send" />
 </LinearLayout>

最佳答案

试试这个 用以下内容替换您的 xml 代码。

我在这里添加了 android:layout_above="@+id/linearlayout_id"RatingBar 并且把你的按钮放在 LinearLayoutandroid:layout_weight="1" 两个按钮

<RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearlayout_id"
        android:layout_alignLeft="@+id/editText3"
        android:layout_marginBottom="21dp" />

    <LinearLayout
        android:id="@+id/linearlayout_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal"
        android:layout_marginBottom="18dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@String/Clear" />

        <Button
            android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@String/Send" />
    </LinearLayout>

这应该有效

关于android - 在相对布局中格式化按钮大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18942934/

相关文章:

android - 使线性布局像 ListView 中的列表项一样可选择(Android)

android - 设置 Chrome 自定义标签时出现 "Please install Android Support Repository"警告

android - 在 Linux 平台上运行 Android 应用程序

javascript - 在javascript中更改所有按钮的背景颜色

android - 在 Android 中根据条件隐藏和显示布局

android - 如何设置 View 权重?

android - phonegap iOS 文件路径

android - 在android ndk中链接静态库

excel - 在所有工作表中应用按钮+宏

php - 标题菜单php中的按钮