2015-10-27 77 views
0

我在我的android應用程序中有這個簡單的活動,但我不知道如何放置我的按鈕,因此它出現在各種屏幕尺寸的相同位置。Android佈局對象放置

我希望按鈕始終放在屏幕的底部。顯示的屏幕截圖來自手機屏幕。當顯示在更大的屏幕上時,按鈕位於更高的位置。

我該如何解決這個問題?

感謝

See this screenshot

回答

0

這是我落得這樣做。感謝所有發佈的人,它幫助我朝着正確的方向前進!

<?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> 
0

您應該使用RelativeLayout

如果你有一個RelativeLayout充滿整個屏幕,你應該能夠使用android:layout_alignParentBottom的按鈕移動到屏幕底部。

如果底部的按鈕沒有顯示,那麼上面的佈局可能會佔用所有空間。在這種情況下,您可以先將按鈕放置在佈局文件中,然後使用android:layout_above將佈局的其餘部分放在視圖上方。

0

製作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> 

希望這本書能解決你的問題

0

看一看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> 
1

可以使用的RelativeLayout用這個參數(機器人:layout_alignParentBottom = 「真」)

<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>