在Android中将TextView显示在屏幕底部

11
在我的应用程序中,我想要在屏幕顶部显示一个宽度为屏幕宽度的TextView。 接下来想要在该TextView下面显示图形视图。 接下来,在Graphical View下方显示一个Text View。
我使用了以下xml代码。 在我的代码中,TextView无法显示在屏幕底部。
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView  
android:orientation="horizontal"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
android:background="#FF8A11"
android:padding="8dip"
/>
</LinearLayout>



<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<com.game.viewdot.ViewDot
 android:id="@+id/DrawView"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />   
 </LinearLayout>



 <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_gravity="bottom"  
>

<TextView  

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Another Text View"
android:background="#FF8A11"
android:padding="8dip"
android:gravity="bottom"
 android:layout_gravity="bottom"
/>

</LinearLayout>

我要对这段代码进行什么修改才能得到我的结果????


1
为什么不使用RelativeLayout?再次编辑布局,以便可以正确查看。 - Tanmay Mandal
4个回答

19

试试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="bottom">

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/ContentView"
android:layout_gravity="bottom" android:layout_alignParentBottom="true"/>

</RelativeLayout>

只是好奇,你为什么需要 android:layout_gravity="bottom" - Francis Saa-Dittoh

8

使用相对布局(RelativeLayout)替代线性布局(LinearLayout)作为父布局。尝试使用以下代码:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/relative01"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        android:background="#FF8A11" 
        android:padding="8dip"
        android:id="@+id/textView01"
        />
        <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/textView01"
    >
    <com.game.viewdot.ViewDot
 android:id="@+id/DrawView"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />   
     </LinearLayout>



     <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    >

    <TextView  

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Another Text View"
    android:background="#FF8A11"
    android:padding="8dip"

    />

    </LinearLayout>

    </RelativeLayout>

希望这能对您有所帮助... :)

但作为一名开发者,我想知道是否可以通过线性布局来实现这一点? - PRATEEK BHARDWAJ
@PrateekBhardwaj 是的,您也可以使用线性布局来实现相同的效果。 - Dinesh Sharma

0

设置所有LinearLayout的高度

android:layout_height="fill_parent"

如果我将“fill parent”设置为所有布局,那么顶部的TextView就会显示。 - ios developer

0

为除TextView之外的所有View设置android:layout_height="fill_parent"属性....


如果我这样设置,意味着只有顶部的TextView会被显示。 - ios developer
你用过RelativeLayout吗?我相信它会很好用...请编辑你的代码,因为有些代码不可见。 - Niranj Patel
我使用相对布局,现在我的顶部TextView无法显示。我使用了上面提到的属性来设置顶部TextView。<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:background="#FF8A11" android:padding="8dip" android:layout_alignParentTop="true" /> - ios developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接