【问题标题】:How to dynamically change button size?如何动态更改按钮大小?
【发布时间】:2020-06-19 11:04:56
【问题描述】:

我正在尝试使两个按钮的大小相等,但是当我更改下面的屏幕按钮大小增加时,您可以在图像中看到。

Click here to view Image

代码如下:

    <?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:background="@android:color/black"
    tools:context=".Timer">

    <Button
        android:id="@+id/player1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#696969"
        android:text="00:00"
        android:textSize="95sp" />

    <RelativeLayout
        android:id="@+id/rl1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/player1">

        <ImageButton
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_centerInParent="true"
            android:src="@drawable/ic_baseline_pause_circle_outline_24"
            android:scaleType="centerCrop"
            android:background="@android:color/transparent"/>
    </RelativeLayout>

    <Button
        android:id="@+id/player2"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_below="@+id/rl1"
        android:layout_alignParentBottom="true"
        android:background="#696969"
        android:text="00:00"
        android:textSize="95sp" />
</RelativeLayout>

【问题讨论】:

标签: android android-studio android-layout


【解决方案1】:

使用 LinearLayout 作为父级,并为子级提供权重以实现结果。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@android:color/black"
android:orientation="vertical"
tools:context=".Timer">

<Button
    android:id="@+id/player1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#696969"
    android:text="00:00"
    android:layout_weight="0.3"
    android:textSize="95sp" />

<RelativeLayout
    android:id="@+id/rl1"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_baseline_pause_circle_outline_24"
        android:scaleType="centerCrop"
        android:background="@android:color/transparent"/>
</RelativeLayout>

<Button
    android:id="@+id/player2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#696969"
    android:text="00:00"
    android:layout_weight="0.3"
    android:textSize="95sp" />

【讨论】:

  • 它有效,但你能告诉我线性布局权重是如何工作的吗?
  • 好的。您可以使用 android:weightSum="10" 为您的父线性布局指定 weightSum。默认情况下,weightSum 为 1。这个权重可以根据方向分布在 LinearLayout 的子视图中。例如,如果要水平放置两个按钮,创建一个水平方向的 LinearLayout,包含两个按钮,为每个按钮添加 android:layout_weight="0.5"。在此处查看更多信息:developer.android.com/guide/topics/ui/layout/linear#Weight
  • 谢谢,我明白了。
猜你喜欢
  • 2018-01-18
  • 2018-02-14
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
相关资源
最近更新 更多