首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表中的按钮在应该match_patent时显示为wrap_content

表中的按钮在应该match_patent时显示为wrap_content
EN

Stack Overflow用户
提问于 2015-04-20 02:22:15
回答 1查看 38关注 0票数 0

对于同一个活动,我有两个布局。这是一个景观,应该有两排按钮,每行有两个按钮(2X2网格),并在底部的横幅广告。一切都已正确定位,但对于layout_width,按钮似乎仍设置为wrap_content。我希望它们都是相同的大小,而不考虑文本。

我在表格中添加了一个边框,我可以看到它是match_parent的,但按钮的宽度都是每列中最宽的按钮。我希望它们都填充表的单元格,并且表应该填满父窗口。

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<TableLayout
    android:layout_width="match_parent"
    android:id="@+id/table_options"
    android:layout_height="fill_parent"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:layout_centerHorizontal="true">

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/table_border">
    <Button
        android:id="@+id/main_activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="1dip"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:paddingTop="1dip"
        android:onClick="onHuntClick"
        android:drawableLeft="@drawable/ic_hunt"
        style="@style/button_style"
        android:text="@string/main_activity" />

    <Button
        android:id="@+id/other_lists"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="1dip"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:paddingTop="1dip"
        android:onClick="onListsClick"
        android:drawableLeft="@drawable/ic_listit"
        style="@style/button_style"
        android:text="@string/other_lists" />
</TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/table_border">

    <Button
        android:id="@+id/web_site"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="1dip"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:paddingTop="1dip"
        android:onClick="onWebClick"
        android:drawableLeft="@drawable/ic_email_color"
        style="@style/button_style"
        android:text="@string/email_vendor" />


    <Button
        android:id="@+id/invite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="1dip"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:paddingTop="1dip"
        android:onClick="onInviteClick"
        android:drawableLeft="@drawable/ic_invite"
        style="@style/button_style"
        android:text="@string/invite" />
    </TableRow>
</TableLayout>
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/main_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
EN

回答 1

Stack Overflow用户

发布于 2015-04-20 02:32:23

这行得通吗?

代码语言:javascript
复制
<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
                xmlns:android="http://schemas.android.com/apk/res/android"
                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">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/main_ad_unit_id">
    </com.google.android.gms.ads.AdView>

    <LinearLayout
        android:id="@+id/table_options"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/adView"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:paddingBottom="10dp"
        android:paddingTop="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/table_border"
            android:orientation="horizontal">

            <Button
                android:id="@+id/main_activity"
                style="@style/button_style"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableLeft="@drawable/ic_hunt"
                android:onClick="onHuntClick"
                android:paddingBottom="1dip"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                android:paddingTop="1dip"
                android:text="@string/main_activity"/>

            <Button
                android:id="@+id/other_lists"
                style="@style/button_style"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableLeft="@drawable/ic_listit"
                android:onClick="onListsClick"
                android:paddingBottom="1dip"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                android:paddingTop="1dip"
                android:text="@string/other_lists"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/table_border"
            android:orientation="horizontal">

            <Button
                android:id="@+id/web_site"
                style="@style/button_style"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableLeft="@drawable/ic_email_color"
                android:onClick="onWebClick"
                android:paddingBottom="1dip"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                android:paddingTop="1dip"
                android:text="@string/email_vendor"/>


            <Button
                android:id="@+id/invite"
                style="@style/button_style"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableLeft="@drawable/ic_invite"
                android:onClick="onInviteClick"
                android:paddingBottom="1dip"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                android:paddingTop="1dip"
                android:text="@string/invite"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

基本上,我所做的就是将这两个按钮放在一个宽度等于父对象的水平方向的LinerLayout中,并为每个按钮赋予参数weight="1“。这意味着它们将具有相同的宽度(父对象的50%)。如果你想让一个的宽度是另一个的两倍,你可以在其中一个上使用weight="2“,另一个则会得到weight="1”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29733935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档