【问题标题】:Move buttons to bottom half on android在android上将按钮移动到下半部分
【发布时间】:2014-08-05 01:27:24
【问题描述】:

我一直在尝试找到一种方法将我的按钮移动到我的 xml 文件的下半部分。目前他们处于上半区。

我正在尝试遵循此处找到的此解决方案,但恐怕它不适用。

Put buttons at bottom of screen with LinearLayout?

我的buttons.xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:weightSum="1">

    <TableLayout android:id="@+id/tableLayout1"
                 android:layout_height="wrap_content"
                 android:layout_width="fill_parent" />

    <Button
        android:id="@+id/block_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_weight="0.09"
        android:background="#ff5ac3ff"
        android:drawableLeft="@drawable/ic_action"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/block_apps"
        android:textSize="22sp" />

    <Button
        android:id="@+id/security_settings_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:layout_weight="0.09"
        android:drawableLeft="@drawable/ic_settings"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/security_settings"
        android:textSize="22sp"/>

    <Button
        android:id="@+id/blacklist_whitelist_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:drawableLeft="@drawable/ic_blacklist_red"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/blacklist_whitelist"
        android:textSize="22sp"
        android:layout_weight="0.09"/>


</LinearLayout>

【问题讨论】:

  • 我建议你将整个布局分成6个相等的LinearLayouts,然后在最后三个布局中设置按钮..你可以问我是否需要任何帮助..跨度>
  • 我确定这不是唯一的解决方案,对吧?但是是的,我们将不胜感激
  • 我已将其添加为我的答案..

标签: android xml android-layout


【解决方案1】:

你应该尝试这样的事情。基本上,如果您使用 layout_weight,则需要将高度或宽度(取决于或您的方向:垂直或水平)设置为“0dp”。否则,视图将不会使用 weight 属性。试试这个 xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/layout"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent">

        <TextView android:id="@+id/emptySpace"
                  android:layout_height="0dp"
                  android:layout_width="fill_parent"
                  android:layout_weight="3" />

        <Button
            android:id="@+id/block_button"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="right|center_vertical"
            android:layout_weight="1"
            android:background="#ff5ac3ff"
            android:drawableLeft="@drawable/ic_action"
            android:gravity="center_horizontal|center_vertical"
            android:text="@string/block_apps"
            android:textSize="22sp" />

        <Button
            android:id="@+id/security_settings_button"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#ff5ac3ff"
            style="?android:attr/borderlessButtonStyle"
            android:layout_weight="1"
            android:drawableLeft="@drawable/ic_settings"
            android:gravity="center_horizontal|center_vertical"
            android:text="@string/security_settings"
            android:textSize="22sp"/>

        <Button
            android:id="@+id/blacklist_whitelist_button"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#ff5ac3ff"
            style="?android:attr/borderlessButtonStyle"
            android:drawableLeft="@drawable/ic_blacklist_red"
            android:gravity="center_horizontal|center_vertical"
            android:text="@string/blacklist_whitelist"
            android:textSize="22sp"
            android:layout_weight="1"/>
    </LinearLayout>

顺便说一句,我将您的表格布局更改为空文本视图,但您应该可以在那里使用任何其他类型的视图。它只是用来填满空间。

此外,使用 layout_weights 时,请注意超出屏幕尺寸的布局(如滚动视图)。那会减轻你的体重。布局权重最适合已知的布局尺寸。

【讨论】:

  • 两个答案有效,我要找出哪个是最好的
【解决方案2】:

我建议你将整个布局分成6个相等的LinearLayouts,然后将按钮设置在最后三个布局中..

试试这个代码..

<LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent">
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <Button
        android:id="@+id/block_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_weight="0.09"
        android:background="#ff5ac3ff"
        android:drawableLeft="@drawable/ic_action"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/block_apps"
        android:textSize="22sp" />

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

     <Button
        android:id="@+id/security_settings_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:layout_weight="0.09"
        android:drawableLeft="@drawable/ic_settings"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/security_settings"
        android:textSize="22sp"/>

  </LinearLayout>
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
     <Button
        android:id="@+id/blacklist_whitelist_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:drawableLeft="@drawable/ic_blacklist_red"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/blacklist_whitelist"
        android:textSize="22sp"
        android:layout_weight="0.09"/>

   </LinearLayout>
</LinearLayout>

请告诉我输出是什么..

【讨论】:

  • 下面的答案也很有效。但是,是的,这也是有效的
【解决方案3】:

我已经对此进行了测试,它应该可以工作,您必须更改按钮属性,因为我使用的是测试内容和图像。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <TableLayout android:id="@+id/tableLayout1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="bottom"
    android:orientation="vertical">
    <Button
        android:id="@+id/block_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/block_apps"
        android:textSize="22sp" />

    <Button
        android:id="@+id/security_settings_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center_horizontal|center_vertical"
        android:text="security settings"
        android:textSize="22sp"/>

    <Button
        android:id="@+id/blacklist_whitelist_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center_horizontal|center_vertical"
        android:text="black white list"
        android:textSize="22sp"/>
</LinearLayout>

</LinearLayout>

【讨论】:

  • 不幸的是没有改变
猜你喜欢
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多