简体   繁体   English

Android文本视图未以相对布局显示。

[英]Android textview is not showing up in relative layout.

I am having a bit of trouble with android relative layout. 我在使用android相对布局时遇到了一些麻烦。 I want the textview with the text "Maze" to appear in the center of two buttons: quit and play. 我希望文本视图“ Maze”出现在两个按钮的中央:退出并播放。 However, the text is not appearing. 但是,该文本没有出现。

    <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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ui.AMazeActivity" >
    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#333333"
        android:layout_alignBottom="@+id/play"
        android:layout_alignParentTop="true"/>
    <Button
        android:id="@+id/quit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="@string/quit_button_text"
        android:textColor="@android:color/white"/>
    <Button 
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:text="@string/play_button_text"
        android:textColor="@android:color/white"/>
    <TextView 
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_toStartOf="@+id/quit"
        android:layout_toEndOf="@+id/play"
        android:layout_alignBottom="@+id/quit"
        android:layout_alignTop="@+id/quit"
        android:text="@string/maze_textview_text"
        android:textColor="@android:color/white"/>
</RelativeLayout>

Here is the picture: (Note that Maze text should appear in between quit and play) 这是图片:(请注意,迷宫文字应出现在退出和游戏之间)

在此处输入图片说明

Try this: 尝试这个:

<TextView 
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_toStartOf="@+id/play"
    android:layout_toEndOf="@+id/quit"
    android:layout_alignBottom="@+id/quit"
    android:layout_alignTop="@+id/quit"
    android:text="@string/maze_textview_text"
    android:textColor="@android:color/white"/>

Use this. 用这个。

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ui.AMazeActivity" >

<View
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/play"
    android:layout_alignParentTop="true"
    android:background="#333333" />

<Button
    android:id="@+id/quit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:text="quit"
    android:textColor="@android:color/white" />

<Button
    android:id="@+id/play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:text="play"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/play"
    android:layout_alignBottom="@+id/play"
    android:layout_centerHorizontal="true"
    android:text="Maze"
    android:textColor="#fff"
    android:textSize="17sp" />

</RelativeLayout>

For this widget alone, you should use a LinearLayout with it's orientation set to Horizontal. 仅对于此小部件,应该使用LinearLayout并将其方向设置为Horizo​​ntal。

I will write you a bit of code ,just to get you started, but I really don't wanna spend my time writing everything as it should be be.Anyways, I am sure you will figure it out right away. 我将为您入门而编写一些代码,但是我真的不想花我的时间编写应有的一切。无论如何,我敢肯定,您会马上发现它的。

<LinearLayout
  android:orientation="horizontal">
  <!-- And whatever other stuff you have to set,like width ,height,bla bla -->
  <Button></Button>  <!-- This is the Quit button-->   
  <TextView></TextView>  <!-- This is the TextView that shows "Maze"--> 
  <Button></Button>  <!-- This is the Play button-->
</LinearLayout>

So, all in all, by using the LinearLayout with orientation set horizontal, every element you add in the layout, will be put to the right of the element added before.The first element, get's added in the left-top corner of the layout. 因此,总而言之,通过将LinearLayout与水平方向一起使用,您在布局中添加的每个元素都将被放置在之前添加的元素的右侧。第一个元素,get被添加在布局的左上角。

If you need more information, let me know via comment and I will help you up. 如果您需要更多信息,请通过评论告知我,我们将为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM