首页 > 解决方案 > Android Studio 将元素与它们下的其他元素对齐

问题描述

您好,我正在使用 android studio 进行移动应用程序开发,我需要制作 3 个LinearLayouts相同的大小。问题是第一个元素包含不同的元素。我需要拉伸第一个元素以使其具有与其他元素完全相同的大小。我想我可以以某种方式知道这是元素的大小并将值设置为第一个元素。也许有可能以某种方式对齐它们?这就是它的外观。感谢帮助!

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    tools:context=".MainView.MainActivity"
    android:orientation="vertical"
    android:paddingTop="100dp">


    <!--    rounds View   -->
    <TextView
        style="@style/option_type_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/rounds" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:id="@+id/minus_round_button"
            style="@style/change_value_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minus" />

        <EditText
            android:id="@+id/round_minutes"
            style="@style/option_input_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/default_time" />

        <Button
            android:id="@+id/plus_round_button"
            style="@style/change_value_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/plus"/>
    </LinearLayout>
    <!--    end rounds View-->
    <!--    work View   -->
    <TextView
        style="@style/option_type_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/work" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:id="@+id/minus_work_button"
            style="@style/change_value_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minus" />

        <EditText
            android:id="@+id/work_minutes"
            style="@style/option_input_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/default_time" />
        <TextView
            style="@style/option_input_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":"/>
        <EditText
            android:id="@+id/work_seconds"
            style="@style/option_input_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/default_time" />
        <Button
            android:id="@+id/plus_work_button"
            style="@style/change_value_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/plus"/>
    </LinearLayout>
    <!--    end Work View-->
    <!--    rest View   -->
    <TextView
        style="@style/option_type_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/rest" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:id="@+id/minus_rest_button"
            style="@style/change_value_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minus" />

        <EditText
            android:id="@+id/rest_minutes"
            style="@style/option_input_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/default_time" />
        <TextView
            style="@style/option_input_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":"/>
        <EditText
            android:id="@+id/rest_seconds"
            style="@style/option_input_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/default_time" />
        <Button
            android:id="@+id/plus_rest_button"
            style="@style/change_value_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/plus"/>
    </LinearLayout>
    <!--    end Rest View-->

    </LinearLayout>

标签: androidxmlandroid-layout

解决方案


使您的第一个元素如下:

<TextView
    style="@style/option_type_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/rounds" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/minus_round_button"
        style="@style/change_value_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/minus" />

    <EditText
        android:id="@+id/round_minutes"
        style="@style/option_input_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/default_time" />
    
    <Button
        android:id="@+id/plus_round_button"
        style="@style/change_value_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/plus"/>
</LinearLayout>

推荐阅读