【发布时间】:2015-10-27 13:33:08
【问题描述】:
我的 android 应用中有这个简单的活动,但我不知道如何放置我的按钮,以便它出现在不同屏幕尺寸的相同位置。
我希望按钮始终位于屏幕底部。显示的屏幕截图来自手机屏幕。当显示在更大的屏幕上时,按钮会更高。
我该如何解决这个问题?
谢谢
【问题讨论】:
标签: android xml android-layout button android-linearlayout
我的 android 应用中有这个简单的活动,但我不知道如何放置我的按钮,以便它出现在不同屏幕尺寸的相同位置。
我希望按钮始终位于屏幕底部。显示的屏幕截图来自手机屏幕。当显示在更大的屏幕上时,按钮会更高。
我该如何解决这个问题?
谢谢
【问题讨论】:
标签: android xml android-layout button android-linearlayout
你可以通过这个参数来使用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>
【讨论】:
你应该使用RelativeLayout
如果您有一个填满整个屏幕的RelativeLayout,您应该可以使用android:layout_alignParentBottom 将按钮移动到屏幕底部。
如果底部的按钮没有显示,那么它上面的布局可能会占用所有空间。在这种情况下,您可以将按钮放在布局文件中的第一个位置,然后将布局的其余部分放在带有android:layout_above 的视图上方。
【讨论】:
看看RelativeLayout。使用这个 ViewGroup 并在其中添加一个 Button 和 alignParentBottom
<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>
【讨论】:
制作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>
希望它能解决你的问题
【讨论】:
这就是我最终要做的。感谢所有发帖的人,它帮助我找到了正确的方向!
<?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>
【讨论】: