มุมมองไม่จัดชิดด้านบนบน LinearLayout

ฉันมี LinearLayout และ XML ของมันคือ:

<?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:layout_gravity="center"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Button" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Button" />
</LinearLayout>

และผลลัพธ์ก็คือ:

ป้อนคำอธิบายรูปภาพที่นี่

อย่างที่คุณเห็นปุ่มซ้ายบนมีระยะขอบเล็กน้อยจากด้านบน แต่เมื่อโค้ดแสดงว่าไม่มีระยะขอบ ทำไมสิ่งนี้ถึงเกิดขึ้น?

นอกจากนี้ยังมีวิธีแก้ไขแปลก ๆ ซึ่งก็คือหากคุณตั้งค่า gravity:top ให้กับทุกปุ่ม คุณจะได้รับผลลัพธ์ตามที่คาดหวัง แต่ทำไมถึงจำเป็นด้วยซ้ำ เพราะ linearlayout(horiz) ควรเริ่มเพิ่มรายการจากซ้ายบนไปขวาบน


person ClassY    schedule 28.05.2021    source แหล่งที่มา


คำตอบ (3)


ฉันกำลังอ้างอิงเอกสารและเธรด SO เพื่อค้นหาวิธีแก้ไขเนื่องจากคำถามนี้ดูน่าสนใจสำหรับฉันมาก

ในที่สุดฉันก็รู้สาเหตุแล้ว

แนวนอน LinearLayout จัดแนวพื้นฐานของการควบคุมลูกทั้งหมดตามค่าเริ่มต้น ดังนั้นบรรทัดแรกของข้อความในปุ่มหลายบรรทัดของคุณจึงถูกจัดแนวในแนวตั้งกับข้อความบรรทัดเดียวในปุ่มอื่นๆ

หากต้องการปิดใช้งานลักษณะการทำงานนี้ ให้ตั้งค่า android:baselineAligned="false" บน LinearLayout

เครดิตทั้งหมดไปที่ @Karu สำหรับคำตอบนี้: https://stackoverflow.com/a/8290258/4211264

person Bhargav Thanki    schedule 28.05.2021

นี่เป็นเพราะการตัดข้อความในปุ่มแรก อีกสองปุ่มใช้พื้นที่บนหน้าจอมากเกินไป และปุ่มแรกพยายามให้อยู่บนหน้าจอโดยการห่อตัวเอง ฉันไม่รู้ว่างานของคุณคืออะไร หากคุณต้องการให้ปุ่มต่างๆ อยู่ในบรรทัดเดียว ให้ลองใช้layout_weight = 1กับทุกปุ่ม

person Konstantin Lysyy    schedule 28.05.2021
comment
สวัสดี. ขอขอบคุณสำหรับการตอบสนองของคุณ. ความสูงถูกตั้งค่าให้ห่อ ไม่ควรยืดปุ่มในแนวตั้งจนใส่ได้ใช่ไหม หากเป็นกรณีนี้เหตุใดจึงได้รับระยะขอบจากด้านบนเนื่องจากไม่เกี่ยวข้องกับความกว้าง - person ClassY; 28.05.2021

เนื่องจากคุณได้ให้น้ำหนักที่แตกต่างกันกับทุกปุ่ม คุณต้องให้น้ำหนักเท่ากันทุกปุ่ม แทนที่โค้ดไฟล์เลย์เอาต์ของคุณด้วยสิ่งนี้

<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:layout_gravity="center"
android:orientation="horizontal"
tools:context=".MainActivity">

<Button
    android:id="@+id/button3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />
person paul035    schedule 28.05.2021
comment
สวัสดี. ขอขอบคุณสำหรับการตอบสนองของคุณ. แต่สิ่งนี้จะให้ความกว้างที่เท่ากันแก่ลูกโดยตรงทุกคน ปัญหาของฉันคือระยะขอบที่ได้รับจากด้านบนซึ่งไม่มีที่ไหนเลยที่จะพบ - person ClassY; 28.05.2021
comment
OP ได้ถามถึงเหตุผลของอัตรากำไรพิเศษที่ด้านบนในปุ่มแรก และขึ้นอยู่กับข้อกำหนด อาจจำเป็นต้องให้น้ำหนักที่แตกต่างกันให้กับวิดเจ็ตทั้งหมด - person Bhargav Thanki; 28.05.2021