【问题标题】:How to make a button and colored rectangle side by side on android?如何在android上并排制作一个按钮和彩色矩形?
【发布时间】:2019-01-07 06:34:20
【问题描述】:

我正在尝试并排制作一个按钮和一个彩色矩形。我不确定是否需要使用线性布局、网格布局或其他方式来执行此操作。这是我尝试过的:

<android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_row="0"
            app:layout_column="0"
            android:text="Button" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_row="1"
            app:layout_column="0"
            android:text="Button" />

        <View
            android:id="@+id/view"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:background="@android:color/holo_blue_bright"
            app:layout_column="1"
            app:layout_gravity="fill"
            app:layout_row="0" />

        <View
            android:id="@+id/view1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:background="@android:color/holo_green_dark"
            app:layout_column="1"
            app:layout_gravity="fill"
            app:layout_row="1" />
    </android.support.v7.widget.GridLayout>

这会产生以下图像:

顶部看起来正确。我想要一个薄按钮,旁边有一个同样薄的矩形。我无法使底部工作。执行此操作的最有效方法是什么,最好以编程方式添加新行(从 2 行开始并添加更多行)。

【问题讨论】:

  • 试试这个,将 girdlayout 高度 match_parent 更改为 wrap_content

标签: android gridview view alignment


【解决方案1】:

目前,为了实现你想要的,这里是代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />


        <View
            android:id="@+id/view"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright" />
    </LinearLayout>


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp">

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


        <View
            android:id="@+id/view1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright" />
    </LinearLayout>


</LinearLayout>

我建议使用 RecyclerView,您可以在其中以编程方式扩充项目视图并将信息绑定到该视图。 RecyclerView 为您管理所有新项目的膨胀和添加。 您可以按照此tutorial 进行设置。

您可以阅读如何实现这一目标,这是您的列表项的外观

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />


        <View
            android:id="@+id/view"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright" />

</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多