【问题标题】:Android button height安卓按钮高度
【发布时间】:2015-10-08 16:47:16
【问题描述】:

我创建了一组使用按钮来表示选项卡的垂直选项卡,并设置了按钮的高度以包裹内容。

这似乎没有得到尊重,尽管按钮缩放以占用所有可用空间,垂直线性布局中只有 5 个按钮,但它们都拉伸以占用可用空间。按钮中的图标很小,文字也很小,所以看起来不太好。

如何让按钮真正只包装其内容?我希望它们都具有相同的大小,并且不会填满所有可用空间。

在 dp 中明确设置高度是好主意还是坏主意?

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_grey">
<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="8dp">
    <FrameLayout android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="3">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"/>
        <LinearLayout
            android:id="@+id/tab_btn_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <Button
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_weight="1.0"
                android:background="@color/lightest_grey"
                android:id="@+id/patient_tab_btn"
                android:text="Patient"
                android:textSize="7sp"
                android:gravity="bottom|center"
                android:drawableTop="@drawable/user"
                android:paddingTop="4dp"
                />
            <Button
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_weight="1.0"
                android:background="@color/lightest_grey"
                android:id="@+id/relations_tab_btn"
                android:text="Relations"
                android:layout_marginTop="1dp"
                android:textSize="7sp"
                android:gravity="bottom|center"
                android:drawableTop="@drawable/family"
                android:paddingTop="4dp"
                />
            <Button
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_weight="1.0"
                android:background="@color/lightest_grey"
                android:id="@+id/providers_tab_btn"
                android:text="Providers"
                android:layout_marginTop="1dp"
                android:textSize="7sp"
                android:gravity="bottom|center"
                android:drawableTop="@drawable/doctors_bag"
                android:paddingTop="4dp"
                />
            <Button
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_weight="1.0"
                android:background="@color/lightest_grey"
                android:id="@+id/locations_tab_btn"
                android:text="Locations"
                android:layout_marginTop="1dp"
                android:textSize="7sp"
                android:gravity="bottom|center"
                android:drawableTop="@drawable/map_marker"
                android:paddingTop="4dp"
                />
            <Button
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_weight="1.0"
                android:background="@color/lightest_grey"
                android:id="@+id/summary_tab_btn"
                android:text="Summary"
                android:layout_marginTop="1dp"
                android:textSize="7sp"
                android:gravity="bottom|center"
                android:drawableTop="@drawable/treatment_plan"
                android:paddingTop="4dp"
                />
            <Button
                android:layout_height="|"
                android:layout_width="match_parent"
                android:layout_weight="1.0"
                android:background="@color/lightest_grey"
                android:id="@+id/cpis_tab_btn"
                android:text="Protection"
                android:layout_marginTop="1dp"
                android:textSize="7sp"
                android:gravity="bottom|center"
                android:drawableTop="@drawable/students"
                android:paddingTop="4dp"
                />

        </LinearLayout>
    </FrameLayout>
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="17"
        android:background="@color/white"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>

谢谢

【问题讨论】:

  • 你能发布你的xml布局吗?
  • 显式设置高度对于包含文本的内容通常不是一个好主意,因为用户可以在您的应用程序之外缩放全局文本大小。要回答其余的问题,您需要发布一些代码 - 您的布局应该足够了,但我们可能需要更多;_0
  • 好的,我已经添加了xml布局,希望够用了!

标签: android button layout


【解决方案1】:

在 dp 中设置高度不会在不同的屏幕上保持完美的比例。

对我来说,最简单的方法是通过代码创建我的界面,这样我就可以进行任何我需要的计算并在 px 上设置确切的大小。

看看my post征求意见。

至于修复您的 xml,将您的 LinearLayout 高度设置为 wrap_content 或删除权重,它使按钮平均分配父高度。

希望这会有所帮助。

【讨论】:

  • 非常感谢帮助移除重量固定它,很遗憾我不能接受多个答案,但再次感谢您的快速回复!
【解决方案2】:

按钮的 layout_weight 参数是导致您的问题的原因。如果设置了此参数,则忽略高度参数。设置权重会导致将所有剩余空间分配给所有具有权重比例的子代。在您的布局中,您有 6 个按钮。因此,剩余空间由 6 个为所有按钮设置的高度相同的按钮填充。您可以限制线性布局父级的高度,或删除按钮的权重。 编辑:我已经提到,如果设置了重量,则会忽略高度。仅当 LinearLayout 的方向是垂直时才适用。如果是水平的,则忽略宽度属性。

【讨论】:

    猜你喜欢
    • 2011-01-18
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多