【问题标题】:LinearLayout dividers are not showing [duplicate]LinearLayout 分隔线未显示[重复]
【发布时间】:2013-05-28 22:07:15
【问题描述】:

我正在开发一个 android 项目,我有一个 LinearLayout,其中包含 2 个使用无边框按钮样式的水平按钮。

我试图在每个按钮之间显示分隔线,但它们没有显示,但我看不出有任何不显示的原因。下面是 XML 布局:

<LinearLayout android:id="@+id/call_log_select_host_button_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:divider="#ffffff"
        android:showDividers="middle"
        android:dividerPadding="22dp">
        <Button android:id="@+id/call_log_select_btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"
            style="?android:attr/borderlessButtonStyle" />
        <Button android:id="@+id/call_log_select_btnBlock"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Block"
            style="?android:attr/borderlessButtonStyle" />
    </LinearLayout>

感谢您提供的任何帮助。

【问题讨论】:

  • 一个愚蠢的问题,但你的背景是什么颜色?
  • 它是深色背景的 Theme.Holo.Dialog
  • 别忘了令人气愤的SHOWDIVIDERS项!!!!!!

标签: android android-linearlayout


【解决方案1】:

res/drawable内创建一个文件mydivider.xml,并放入如下形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:width="1dip" />
    <solid android:color="#ffffff" />
</shape>

将形状添加为布局的分隔符

<LinearLayout android:id="@+id/call_log_select_host_button_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:divider="@drawable/mydivider"
        android:showDividers="middle"
        android:dividerPadding="22dp">
        <Button android:id="@+id/call_log_select_btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"
            style="?android:attr/borderlessButtonStyle" />
        <Button android:id="@+id/call_log_select_btnBlock"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Block"
            style="?android:attr/borderlessButtonStyle" />
    </LinearLayout>

或作为解决方法:

<LinearLayout android:id="@+id/call_log_select_host_button_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:divider="@drawable/mydivider"
        android:showDividers="middle"
        android:dividerPadding="22dp">
        <Button android:id="@+id/call_log_select_btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"
            style="?android:attr/borderlessButtonStyle" />

         <View    android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:background="#ffffff" />

        <Button android:id="@+id/call_log_select_btnBlock"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Block"
            style="?android:attr/borderlessButtonStyle" />
    </LinearLayout>

【讨论】:

  • 这在 android 2.2 上不起作用。
  • @BasavarajHampali 但您可以使用支持库中的 LinearLayoutICS,如下所示:stackoverflow.com/questions/22547897/…
  • 它不适用于面向 Android 4.4.2 且最小 API 11 的应用
  • @legr3c:不要进行垃圾编辑。
  • 除了编写自己的 mydivider.xml 文件之外,您还可以从当前主题中引用一个,即 android:divider="?android:attr/dividerHorizo​​ntal" 或 android:divider="?android: attr/dividerVertical" - 适用于 API 11 及更高版本。
【解决方案2】:

尝试将android:divider="#ffffff" 替换为android:divider="@android:color/white"。我怀疑divider 必须是 Drawable,并且硬编码颜色可能不会将其视为 Drawable,但引用它可能会。

【讨论】:

  • 你是对的。从颜色切换到可绘制,瞧!在职的。接受的答案有很好的例子
【解决方案3】:

您应该在 LinearLayout 中添加 android:layout_width="fill_parent"android:layout_height="fill_parent"。这样它就会显示;)

【讨论】:

  • 这样做会破坏布局,因为按钮组上方的内容不会仅显示活动顶部的按钮,并且分隔线仍然没有显示
  • 并在您的 LinearLayout 中添加android:weightSum=""?这能解决你的问题吗?
  • 我已尝试将 weightSum 设置为 2,如 stackoverflow.com/questions/7452741/… 中所述,但它仍然显示顶部的按钮组占据顶部,其中应该位于按钮上方的内容未显示且分隔线没有显示
  • style="?android:attr/borderlessButtonStyle" 仅在我正确的情况下在 Android 4.0 中实现。你把它设置为android 4.0+吗?编辑:它在 API 级别 11 中实现
  • 我的目标是 Jelly Bean,按钮无边框属性按预期工作,只是分隔线不是
猜你喜欢
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 2014-08-11
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 2013-12-05
  • 1970-01-01
相关资源
最近更新 更多