【问题标题】:How to make button in listview fit any screen如何使列表视图中的按钮适合任何屏幕
【发布时间】:2017-02-21 21:04:43
【问题描述】:

我正在处理一个 xml 代码,但我遇到了 2 个问题,a。使按钮适合屏幕 b.删除按钮之间的空间。如何使列表视图中的按钮适合任何屏幕,以及如何删除按钮之间的空格。请在回答时编辑我的代码,谢谢

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/content_main"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:context="org.cepfonline.lwponlineforum.activities.MainActivity"
   tools:showIn="@layout/app_bar_main">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <Button
            android:id="@+id/button01"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <Button
                android:id="@+id/Button02"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>

        </LinearLayout>

        <Button
            android:id="@+id/button03"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.00"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button04"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />

        </LinearLayout>

        <Button
            android:id="@+id/button05"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.00"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button06"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android xml listview button


    【解决方案1】:

    我认为默认android按钮之间的差距或边距来自按钮中使用的背景图片。

    第一个选项 您可以像这样使用 android 按钮样式

    <Button
            android:id="@+id/button01"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            style="?android:attr/buttonBarButtonStyle"
            android:background="@color/colorPrimary"
            android:layout_weight="1"/>
    

    第二个选项是在每个按钮上使用背景颜色。所以完整的代码是这样的

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button01"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            android:layout_weight="1"/>
    
        <Button
            android:id="@+id/Button02"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            android:layout_weight="1"/>
    
        <Button
            android:id="@+id/button03"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            android:layout_weight="1"/>
    
        <Button
            android:id="@+id/button04"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            android:layout_weight="1" />
    
    </LinearLayout>
    

    前后对比图

    【讨论】:

    • 不使用背景色技巧我不能关闭空间
    • 不,因为我认为它来自默认背景图像。可能带有透明图像边框
    • 哪个默认背景图片,我没有使用任何,如果有默认来自哪里以及如何删除它
    • 我不确定,但你可以检查android源代码中的按钮视图类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多