簡體   English   中英

為什么drawableStart的行為與Android文檔不匹配?

[英]How come the behavior of drawableStart doesn't match the Android documentation?

我創建了一個非常基本的布局:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            android:drawableStart="@drawable/ic_launcher" />

    </LinearLayout>
</RelativeLayout>

根據drawableStart的文檔

“可以在文本開頭繪制的可繪制內容。”

但是,當我在Android 4.0.4手機上運行時,我會看到:

在此輸入圖像描述

為什么圖標和文字之間有這么大的差距? 根據這個答案

“使用Android 4.0(API級別14),您可以使用android:drawableStart屬性在文本的開頭放置一個drawable。”

但這不是我觀察到的行為。 為什么屬性不起作用?

開始和結束都有很多誤解。
布局xml中的開始結束右的替代,以匹配布局方向(LTR或RTL)。

所以,當文件說:

“可以在文本開頭繪制的可繪制內容。”

你必須閱讀:

“根據布局方向繪制到視圖開頭的drawable”

原因是因為drawableStart使按鈕成為復合布局,即圖像視圖和TextView全部包裹在“按鈕”中...

所以你看到的是ImageView被放置在Textview的前面。然而,TextView仍然設置了它的默認布局屬性,以便它將它繪制在左邊的空間中心,因此間隙(注意它在空間中居中)通過將圖像放在文本的開頭而離開)

所以你基本上需要覆蓋按鈕TextView的重力屬性 -

android:gravity="center_vertical|center_horizontal|left"

請看下面的注意事項,你只需要將按鈕包裹在1個布局中,另一個是冗余的...即RelativeLayout也可以是一個LinearLayout,因為你在布局中只有一個視圖!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableStart="@drawable/ic_launcher"
        android:gravity="center_vertical|center_horizontal|left"
        android:text="@string/app_name" />

</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM