Java 如何区分二者;“意见”;避免相互重叠
我是android编程的初学者,正在尝试构建一个简单的应用程序。 每当我尝试创建多个视图时,它们会相互堆叠。 我试图创建一个文本字段和一个按钮,但是当我运行它时,文本字段和按钮彼此重叠,但是,我希望它们之间有一定的距离。 我正在使用以下代码Java 如何区分二者;“意见”;避免相互重叠,java,android,android-layout,Java,Android,Android Layout,我是android编程的初学者,正在尝试构建一个简单的应用程序。 每当我尝试创建多个视图时,它们会相互堆叠。 我试图创建一个文本字段和一个按钮,但是当我运行它时,文本字段和按钮彼此重叠,但是,我希望它们之间有一定的距离。 我正在使用以下代码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.new1.MainActivity" >
<EditText android:id="@+id/edit_message"
android:layout_weight= "1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</RelativeLayout>
我正在三星Galaxy S2上检查此代码。
是否有人知道这个问题的解决方案,并可以指导我哪里做错了。可以使用
线性布局
使元素水平或垂直堆叠,或者使用属性,如android:layout\u toRightOf=“@id/edit\u message”
来控制RelativeLayout
中的位置
了解有关
LinearLayout
的更多信息,请单击此处;以及RelativeLayout
请单击此处:根据您需要的布局(水平布局或垂直布局),您需要合适的布局xml
您可以使用relativeLayout,其中必须指定要布局的父级,如:
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
如果你的初学者最好看看垂直或水平模式下的线性布局
那么你不必指定这个
像这样:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bResults"
android:text="Try Command"
android:layout_weight="20" />
<ToggleButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tbPassword"
android:layout_weight="80"
android:checked="true"
/>
</LinearLayout>
这将使两个按钮彼此相邻相对位置视图相对位置。因此,如果不指定视图之间的关系,则所有视图将一个置于另一个之上。可以将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" >
<EditText
android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="@string/edit_message"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/edit_message"
android:text="@string/button_send" />
</RelativeLayout>
这里android:layout_toRightOf=“@id/edit_message”可以让你的按钮定位在编辑文本的右侧非常感谢dave,我还没有查看后面的按钮,但是“linearlayout”对我来说很有用。我想你必须学会如何在android中进行设计。