【问题标题】:Android Studio aligning elements to other elements under themAndroid Studio 将元素与它们下的其他元素对齐
【发布时间】:2020-08-01 22:09:24
【问题描述】:

您好,我正在使用 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>

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    按如下方式制作您的第一个元素:

    <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>
    

    【讨论】:

    • 它不工作。它将第一个元素拉伸到屏幕大小:/
    • 尝试在设计选项卡中扩展它,或者在第一轮中添加 :00
    猜你喜欢
    • 2014-12-01
    • 2010-09-19
    • 2021-11-04
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2015-12-20
    相关资源
    最近更新 更多