Android 布局对象放置

Android layout objects placement

我的 android 应用程序中有这个简单的 activity,但我不知道如何放置我的按钮,以便它在各种屏幕尺寸上出现在相同的位置。

我希望按钮始终位于屏幕底部。显示的屏幕截图来自 phone 屏幕。当显示在更大的屏幕上时,按钮会更高。

我该如何解决这个问题?

谢谢

See this screenshot

你应该使用 RelativeLayout

如果 RelativeLayout 填满了整个屏幕,您应该可以使用 android:layout_alignParentBottom 将按钮移动到屏幕底部。

如果您底部的按钮没有显示,那么它上面的布局可能占用了所有 space。在这种情况下,您可以将按钮放在布局文件的第一个位置,然后使用 android:layout_above 将布局的其余部分放在视图上方。

制作footerButton.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"       
    android:gravity="center" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="HORAIRE"
        android:textColor="#ffffff"
        android:textSize="20sp" />

</RelativeLayout>

并将其添加到您的所有布局文件中,如下行所示:

Android Activity_Layout

  <RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >



  <!-- Footer aligned to bottom -->
  <include layout="@layout/footer" />

  <!-- Content above footer -->
  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/footer"
    android:layout_below="@id/header"
    android:gravity="center">

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Content goes here"
      android:textColor="#FFFFFF"
      android:textSize="20sp" />
  </RelativeLayout>

</RelativeLayout>

希望能解决您的问题

看看RelativeLayout。使用此 ViewGroup 并在其中添加一个 ButtonalignParentBottom

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    <Button
        android:id="@+id/button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alignParentBottom="true"
        android:text="HORAIRE"
        android:layout_margins="<your_margin_in_dp>" />

</RelativeLayout>

您可以通过此参数使用 RelativeLayout (android:layout_alignParentBottom="true")

<RelativeLayout 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

    <Button 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:text="Click Me!"/>  

</RelativeLayout>   

这就是我最终所做的。感谢所有发帖的人,它帮助我找到了正确的方向!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context="xxxx.MainActivity"
    android:orientation="vertical"
    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dernières nouvelles Twitter"
        android:id="@+id/textView"
        android:textStyle="bold"
        android:textSize="17dp"
        android:paddingLeft="8dp"
        android:paddingTop="6dp"
        android:paddingRight="6dp"
        android:paddingBottom="6dp"
        android:layout_alignParentTop="true"/>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/listView_tweets"
        android:layout_above="@+id/buttonDisplay"
        android:choiceMode="none"
        android:paddingTop="25dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Horaire"
        android:layout_margin="5dp"
        android:id="@+id/buttonDisplay"
        android:onClick="buttonGotoDisplay"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>