简体   繁体   中英

wrap_content does not work in LinearLayout

在此处输入图像描述 The two button in the following code, were set to wrap_content and no padding. However, there is still some padding in the vertical orientation. If I set the android:layout_height to a fixed parameter, the padding could disappear. But I do want to use wrap_content instead of a fixed height.

EDIT: Now, I have to use DIP, which is not recommended, as text size to ensure the text in the button while using a fixed height in DIP. Looking forward to a better solution!

<LinearLayout 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginBottom="5dp">

                <LinearLayout 
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:layout_marginLeft="5dp"
                    android:orientation="vertical">

                    <TextView 
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/account_balance"/>

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

                        <TextView 
                            android:id="@+id/tvBa"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"/>

                        <ImageButton 
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="#00000000"
                            android:src="@drawable/video_list_price"/>
                    </LinearLayout>
                </LinearLayout>

                <Button 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/record_selector"
                    android:gravity="center"
                    android:textColor="#ffffffff"
                    android:text="@string/charge_record"/>

                <Button 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:background="@drawable/charge_selector"
                    android:gravity="center"
                    android:textColor="#ffffffff"
                    android:text="@string/charge"/>
            </LinearLayout>

By default container content align top.

            <LinearLayout 
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:layout_marginLeft="5dp"
                android:orientation="vertical">

                <TextView 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/account_balance"/>

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

                    <TextView 
                        android:id="@+id/tvBa"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>

                    <ImageButton 
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="#00000000"
                        android:src="@drawable/video_list_price"/>
                </LinearLayout>
            </LinearLayout>

This content is taking more height compare to Button.

            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/record_selector"
                android:gravity="center"
                android:textColor="#ffffffff"
                android:text="@string/charge_record"/>

So actually buttons are not having padding now, but it is taking more space due to first item of your root LinearLayout.

Add one more attribute in your root LinearLayout. android:gravity="center_vertical"

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal" 
    android:gravity="center_vertical"
    android:background="#756846">

Remove this line:

android:layout_weight="1.0"

And change the 0dp value to wrap_content

--EDITED

You have 2 potential sources of the padding: first one is initial button backgroud sizes - it will not be scaled down, only up

Second one is the default style for button - just set padding = 0dp

Add minHeight and minWidth to your button as 0

 <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/record_selector"
                android:gravity="center"
                android:textColor="#ffffffff"
                android:minHeight="0dp"
                android:minWidth="0dp"
                android:text="@string/charge_record"/>

 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM