【问题标题】:How to show three buttons at the bottom of the screen like facebook in android?如何在屏幕底部显示三个按钮,如android中的facebook?
【发布时间】:2014-07-20 04:11:23
【问题描述】:

我正在使用 android,我需要进行 XML 设计,我需要在屏幕底部始终水平放置三个按钮。就像 android 中的 facebook 应用程序一样,它们在屏幕底部有三个按钮(状态、照片、签入)——同样,我的应用程序中也需要有三个按钮。

这种布局的设计是什么?

我尝试使用下面的 XML 设计,但它无法以某种方式工作,而且它们都是垂直的 -

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/cow"
        android:layout_weight="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="ButtonA" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="ButtonB" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="ButtonC" />
    </LinearLayout>

</LinearLayout>

我只需要在屏幕底部水平显示三个按钮,就像 facebook 总是并排显示(相互触摸)。这可以吗?

按钮名称可以是 - ButtonA, ButtonB and ButtonC

【问题讨论】:

  • 为什么在布局中声明了 6 个按钮?
  • 您可能需要一个带有底部 LinearLayout 按钮的 ScrollView - 在 RelativeLayout 中 buttons.layout_below
  • 它们是垂直的,因为您在 LinearLayout 上有 android:orientation="vertical"。将其更改为水平。
  • 我明白了,但它会像在 Facebook 应用程序中那样彼此靠近吗?我需要让它们尽可能接近,只是试图模仿 facebook 应用程序的外观。
  • 在任一侧添加空的Views,并根据需要调整权重。

标签: java android xml android-linearlayout android-relativelayout


【解决方案1】:

使用下面的布局,我认为它对你有帮助

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="center"
        android:text="cow"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="ButtonA" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="ButtonB" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="ButtonC" />
</LinearLayout>

【讨论】:

    【解决方案2】:

    我不知道你最终需要如何调整它,所以我把它留得很笼统:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <View
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="2" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
    
            <View
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="2" />
    
        </LinearLayout>        
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      也许你可以使用这样的 Header-Footer-Content 结构的布局:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >
      
      <!-- Header aligned to top -->
      <RelativeLayout
          android:id="@+id/header"
          android:layout_alignParentTop="true"
          ...>
      </RelativeLayout>
      
      <!-- Footer aligned to bottom -->
      <RelativeLayout
          android:id="@+id/footer"
          android:layout_alignParentBottom="true"
          ...>
         <!-- put your layout and buttons here -->
      </RelativeLayout>
      
      <!-- Content below header and above footer -->
      <RelativeLayout
          android:id="@+id/content"
          android:layout_above="@id/footer"
          android:layout_below="@id/header"
          ...>
      </RelativeLayout>
      

      您可以随意移除碎片。

      【讨论】:

        猜你喜欢
        • 2014-09-29
        • 2014-11-01
        • 2011-08-10
        • 2012-03-08
        • 2013-07-14
        • 1970-01-01
        • 2016-06-02
        • 1970-01-01
        • 2015-06-05
        相关资源
        最近更新 更多