【问题标题】:Aligning Buttons Dynamically - Android动态对齐按钮 - Android
【发布时间】:2012-12-06 09:33:52
【问题描述】:

我试图让我的按钮水平居中并从顶部垂直向下 2/3。无论屏幕尺寸/密度如何,我都希望它从顶部保持 2/3。这是我要移动的按钮:

<Button
    android:id="@+id/btnResume"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="250dp" />

谢谢!

工作代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <Button
        android:id="@+id/btnResume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <TextView
        android:id="@+id/tvCounter2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>
</LinearLayout>

【问题讨论】:

  • 哪个布局使用了?使用 layout_weight 属性进行线性布局
  • 使用相对布局,但我认为,如果需要,我总是可以在相对布局中放置线性布局。 (抱歉延迟回复...没有看到评论)

标签: android button dynamic position


【解决方案1】:

要存档此内容,您需要使用 LineareLayout 并探索 weight 功能。要让它工作,它必须是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</FrameLayout>

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</FrameLayout>

<Button
    android:id="@+id/btnResume"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</FrameLayout>

</LinearLayout>

希望对你有帮助!

【讨论】:

  • 我不确定如何使用它使我的按钮位于屏幕下方的 2/3。能说的具体点吗?
  • 啊没关系。我只是解释它。谢谢!让我尝试实现它
  • 好的,您不能仅使用按钮的属性将按钮向下移动 2/3。您唯一能做的就是使用LineareLayout 并将weign 属性设置为它的孩子,以将您的按钮设置在适当的位置。如果你的 UI 中有任何其他元素,你可以使用我的代码作为一个层,然后将你所有的东西放在 RelativLayout 中。检查此链接以获取有关 weight 属性 developer.android.com/guide/topics/ui/layout/linear.html#Weight 的更多信息
  • 为你的根LineareLayout设置android:orientation="vertical"标签。默认为水平方向。
  • 非常感谢!我发布了上面的工作代码以供其他人将来参考
【解决方案2】:

你可以使用属性“layout_weight”

【讨论】:

    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 2021-10-23
    • 2017-11-28
    • 2014-02-27
    • 2011-05-12
    • 2011-06-22
    • 2012-02-11
    • 1970-01-01
    相关资源
    最近更新 更多