【问题标题】:Button in relative layout not affected by setting layout margins相对布局中的按钮不受设置布局边距的影响
【发布时间】:2011-06-08 18:42:13
【问题描述】:

带有RelativeLayout 的自定义Dialog 框包含一个Button 小部件,无论方向如何,它都不会改变其边距。这是xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal" android:background="#99000000">
    <TextView android:id="@+id/dialogtitle" android:layout_width="fill_parent"
         android:layout_height="wrap_content" android:text="Some text" />
    <TextView android:id="@+id/dialogtext" android:layout_width="300dp"
        android:layout_height="wrap_content" android:textSize="15dp"
        android:layout_alignParentLeft="true" android:layout_below="@id/dialogtitle"
        android:paddingLeft="8dp" />
    <Button android:id="@+id/dialogbuttoninfo" android:layout_width="80dp"
        android:layout_height="wrap_content" android:text="Doesn't care about margins"
        android:layout_alignParentRight="true" android:layout_marginLeft="128dp" />
</RelativeLayout>

填充有效,但仅移动按钮内的文本。有什么建议吗?

【问题讨论】:

  • 你想做什么?我可以添加一个右边距并且它有效
  • 它是一个对话框,所以也许这解释了可变性
  • 另外,mumps 标签到底是什么?
  • 维基腮腺炎语言。它有点有趣。

标签: android user-interface button dialog


【解决方案1】:

这看起来很糟糕,但您是否尝试过将其全部放入 LinearLayout 中?或者可能删除android:orientation="horizontal"。我不认为RelativeLayout 关心我所看到的方向。我也认为,但可能是错误的,如果你做一个LinearLayout,那么你也不需要在那里有android:layout_alignParentLeft="true"

稍微清理一下之后(抱歉,当它在问题中被压缩时很难阅读)你也没有说明最后一个 TextViewdialogbuttoninfo 应该是相对于一切否则,我认为您必须这样做才能使Relative layouts 正常运行,我发生了一些奇怪的事情。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#99000000">
    <TextView
        android:id="@+id/dialogtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Some text" />
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
        <TextView
            android:id="@+id/dialogtext"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:layout_alignParentLeft="true"
            android:paddingLeft="8dp" />
        <Button
            android:id="@+id/dialogbuttoninfo"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="Doesn't care about margins"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="128dp" />
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 好名字。讽刺指出。我也在读研究生。是的,Llayout 有效,也许我会使用它。 Android UI 工具让我抓狂。无论我做什么,我确信一旦我开始在不同的设备上进行测试,一切都会解开。谢谢你。
  • 哈哈,你自己也挺有名字的。我也对布局的不一致感到沮丧,幸运的是,对于 android 和 java,似乎总是有另一种方法可以完成任何事情。
  • IMO 最好的方法是在 glsurfaceview 中使用 opengl。它基本上是一个没有限制的空白画布,几乎没有'android'。
  • hrm 这很有趣。我必须检查一下,每天在这里,我都会找到完成工作的全新方法。
【解决方案2】:

您可能需要将按钮的左侧与某物对齐,然后边距才真正有意义。

【讨论】:

    猜你喜欢
    • 2013-08-22
    • 2012-10-12
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多