WRAP_CONTENT不LinearLayout中工作

问题描述 投票:0回答:2

下面的代码两个button,分别设置为wrap_content,没有填充。然而,仍然有在垂直方向一些填充。如果我设置android:layout_height为固定参数,填充可能会消失。但我想用的,而不是一个固定的高度wrap_content

编辑:现在,我必须使用DIP,不建议这样做,因为文字大小,以确保在按钮上的文本,而在DIP使用固定的高度。期待一个更好的解决方案!

<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>
android android-layout
2个回答
0
投票

默认情况下,容器内容对齐,顶部。

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

所以实际上按钮现在不具有填充,但由于根的LinearLayout的第一项中取得更大的空间。

在你的根LinearLayout中添加更多的属性。机器人:重力= “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">

0
投票

删除这一行:

android:layout_weight="1.0"

并更改0dp值WRAP_CONTENT

--EDITED

你有2个潜在填充的来源:一是初始按钮背景大小 - 它不会被缩减,最多只

第二个是按钮的默认风格 - 刚刚成立填充= 0dp

© www.soinside.com 2019 - 2024. All rights reserved.