【问题标题】:Fit button width in GridLayout column Android在 GridLayout 列 Android 中调整按钮宽度
【发布时间】:2016-03-09 02:09:10
【问题描述】:

我尝试创建一个 GUI - GridLayout 2 行和 2 列(您可以在图像中看到)。我希望按钮宽度适合列 (2) 宽度。但是结果溢出了。

你遇到过这种问题吗?

这是我的代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:background="@drawable/round_corner_opacity_background"
    android:columnCount="2"
    android:rowCount="6">
    <!--Pick up-->
    <ImageView
        android:layout_column="0"
        android:layout_gravity="center_vertical"
        android:layout_margin="5dp"
        android:layout_row="0"
        android:layout_rowSpan="2"
        android:src="@drawable/ic_ci_pick_up" />

    <TextView
        style="@style/text_view_description"
        android:layout_column="1"
        android:layout_columnWeight="1"
        android:layout_row="0"
        android:text="@string/pick_up" />

    <Button
        android:id="@+id/btnSelectPickUp"
        style="@style/button_location"
        android:layout_column="1"
        android:layout_columnWeight="1"
        android:layout_row="1"
        android:maxLines="3"
        android:singleLine="false"
        android:text="@string/select_pick_up" />
    <!--Separator-->
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_column="1"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_row="2"
        android:background="@color/primary" />
    <!--Drop off-->
    <ImageView
        android:layout_column="0"
        android:layout_gravity="center_vertical"
        android:layout_margin="5dp"
        android:layout_row="3"
        android:layout_rowSpan="2"
        android:src="@drawable/ic_ci_drop_off" />

    <TextView
        style="@style/text_view_description"
        android:layout_column="1"
        android:layout_columnWeight="1"
        android:layout_row="3"
        android:text="@string/drop_off" />

    <Button
        android:id="@+id/btnSelectDropOff"
        style="@style/button_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_columnWeight="1"
        android:layout_row="4"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="@string/select_pick_up" />
    <!--ConfirmedBookings-->
    <Button
        android:id="@+id/btnShowConfirmedBookingsDialog"
        style="@style/button_default"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_marginTop="15dp"
        android:layout_row="5"
        android:text="@string/confirmed_booking" />
</GridLayout>

<Button
    android:id="@+id/btnShowBookDialog"
    style="@style/button_default"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center|bottom"
    android:text="@string/book" />

谢谢,

【问题讨论】:

    标签: android android-gridlayout column-width


    【解决方案1】:

    你能贴一段代码吗?您可以设置宽度来填充该按钮的父级而不是android:layout_columnWeight="1"

    我会使用重量。有了它,您可以在您设置匹配父级的父级中指定您的宽度。

    参考:Android Weights

    同时检查:Shrink and Stretch columns

    【讨论】:

    • 我编辑了我的答案。您应该使用 标签。还要重构你的代码。看起来有点乱。
    • 还取决于您测试的 API 级别:stackoverflow.com/questions/10016343/…
    • 您是否尝试将 columnWeight 更改为 width 并将其设置为填充父级?
    • 是的,我做到了。它仍然溢出:(
    • 并将您的网格布局设置为填充父项而不是匹配父项?这行得通吗?
    猜你喜欢
    • 2016-10-14
    • 2014-01-23
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    相关资源
    最近更新 更多