Java 在RelativeLayout中使用LayoutToLeftof布局的按钮不会显示
我试图在屏幕中央的文本视图左侧布置一个按钮。我的布局如下所示:Java 在RelativeLayout中使用LayoutToLeftof布局的按钮不会显示,java,android,layout,Java,Android,Layout,我试图在屏幕中央的文本视图左侧布置一个按钮。我的布局如下所示: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_he
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo"
android:id="@+id/center"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left button"
android:layout_toLeftOf="@id/center" />
</RelativeLayout>
不幸的是,按钮没有出现。我得到以下结果:
如你所见,按钮没有显示。如果我使用layout_toRightOf,则该按钮将显示在文本视图的右侧,正如预期的那样
你知道我做错了什么吗?试试看
<Button android:id="@+id/button"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left button">
</Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo"
android:layout_toRightOf="@id/button"
android:id="@+id/center">
</TextView>
试试看
TextView
的规则。如果希望TextView
居中,可以这样说(例如,android:layout\u centerInParent=“true”
)按钮的规则
hierarchyviewer
检查布局,找出物品的位置TextView
的规则。如果希望TextView
居中,可以这样说(例如,android:layout\u centerInParent=“true”
)按钮的规则
hierarchyviewer
检查布局,找出物品的位置您也可以将文本填充到您想要的位置…您也可以将文本填充到您想要的位置…r结束,感谢您的回复。如果我听从你的建议,文本视图不再位于屏幕中央。如何确保文本视图居中?谢谢你的回复。如果我听从你的建议,文本视图不再位于屏幕中央。如何确保文本视图居中?谢谢。你的帖子让我意识到我对Android上的布局几乎一无所知。谢谢你提供的有用的提示。@commonware-我意识到这个问题早就有答案了,但令人惊讶的是,这个答案很少给出,而且很难找到。非常感谢你!为我节省了一天的时间,将垃圾重新加工成线性布局。@WesWinn:这个问题太老了,我忘了模拟器在屏幕截图中的样子…:-)不过,很高兴听到这有帮助!你的帖子让我意识到我对Android上的布局几乎一无所知。谢谢你提供的有用的提示。@commonware-我意识到这个问题早就有答案了,但令人惊讶的是,这个答案很少给出,而且很难找到。非常感谢你!为我节省了一天的时间,将垃圾重新加工成线性布局。@WesWinn:这个问题太老了,我忘了模拟器在屏幕截图中的样子…:-)不过,很高兴听到这有帮助!