【问题标题】:Make 4 buttons take a quarter of the screen each让 4 个按钮每个占据屏幕的四分之一
【发布时间】:2016-04-10 17:10:18
【问题描述】:

我希望我的按钮看起来与this 完全一样,但我不知道该怎么做。我曾尝试在谷歌上查看不同的地方,但没有一种方式像我想要的那样。我想让我的按钮有图像,我也想知道如何在不同的屏幕尺寸上调整这些图像的大小。

这是我现在的 layout_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<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="com.mudd.devin.drivenow.MainActivity"
    android:weightSum="2">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button3"
        android:layout_marginTop="199dp">

    </Button>

    <Button

        android:layout_width="wrap_content"
        android:id="@+id/button1"
        android:layout_gravity="center"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

【问题讨论】:

  • 使用具有相等android:layout_weight 值的嵌套LinearLayout 小部件。或者,使用 Android 支持包中的 PercentFrameLayout/PercentRelativeLayout

标签: android xml button


【解决方案1】:

您有 3 个选项,首先是简单的选项:

  1. RelativeLayout 的中心是 View

View 放在RelativeLayout 的中心,并在其周围对齐所有其他视图(toLeftOftoRightOf、...)

  1. 使用带有权重的嵌套LinearLayout

使用一个LinearLayout水平和一个垂直,分别给你的视图宽度和高度0dp并设置所有weight="1"

  1. 编写自定义布局/ViewGroup

只需编写一个简单的布局,在onLayout 中为您的视图分配相等的宽度和高度。这需要更多知识,但如果操作正确,这是最有效的解决方案。

【讨论】:

    【解决方案2】:

    您可以使用 layout_weight 属性来设置屏幕上显示的百分比的权重。

    例如:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="1" >
        </Button>
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="2" >
        </Button>
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="3" >
        </Button>
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="4" >
        </Button>
    </LinearLayout>
    

    【讨论】:

    • 我怎样才能让他们都接触?我的意思是消除它们之间的小间隙。
    • 您可以使用边距或填充来在视图之间留出空间。
    【解决方案3】:

    有点晚了,但您可以只使用一个 RelativeLayout,将一个虚拟对象居中放置,然后围绕该中心点设置其余元素。

    【讨论】:

      【解决方案4】:

      我不知道它是否适用于以前的版本,但在 Android Studio 4.1 中,您可以使用 constraintlayout 并更改按钮的这些属性:

      android:layout_width="0dp"  // match constraint
      android:layout_height="0dp" // match constraint
      app:layout_constraintHeight_percent="0.5"   // 50%
      app:layout_constraintWidth_percent="0.5"    // 50%
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-30
        • 1970-01-01
        • 2013-08-30
        • 2012-07-22
        • 2019-12-17
        • 2014-10-19
        相关资源
        最近更新 更多