【问题标题】:Buttons in android are overlapping. Need a fixandroid中的按钮是重叠的。需要修复
【发布时间】:2011-08-31 05:56:53
【问题描述】:

我的 xml 文件有问题。好吧,起初它可以工作,但后来我想给它一个滚动视图,所以我将滚动视图添加到 main.xml。保存文件后。该应用程序曾经在没有启动的情况下崩溃。看到这一点,我删除了 main.xml 文件上的滚动视图,并面临一个新问题,其中所有按钮都相互重叠。

另外,如果您能告诉我如何正确提供滚动视图标签,那将很有帮助。 干杯。谢谢!

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

<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Creating and Using Databases without Content Provider"/>

<EditText 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edit"
android:padding="6dip"/>

 <EditText 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/msg"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "Add a new book"
android:id="@+id/addTitle"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/upTitle"
android:text="Update a book"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/rtrTitles"
android:text="Retrieve all books"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/rtrTitle"
android:text="Retrieve Specific book"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/delTitle"
android:text="Delete a book"
android:padding="6dip"/>

<Button 
android:text="Send SMS" 
android:id="@+id/sms" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:padding="6dip"/>

</RelativeLayout>

这是按钮重叠的主 xml 文件。

<?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"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Creating and Using Databases without Content Provider"/>

<EditText 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edit"
android:padding="6dip"/>

<EditText 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/msg"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "Add a new book"
android:id="@+id/addTitle"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/upTitle"
android:text="Update a book"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/rtrTitles"
android:text="Retrieve all books"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/rtrTitle"
android:text="Retrieve Specific book"
android:padding="6dip"/>

<Button 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/delTitle"
android:text="Delete a book"
android:padding="6dip"/>

<Button 
android:text="Send SMS" 
android:id="@+id/sms" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:padding="6dip"/>
</ScrollView>
</RelativeLayout>

这是带有滚动视图的 main.xml

【问题讨论】:

标签: android xml android-layout scrollview


【解决方案1】:

您使用的是RelativeLayout,实际上并没有使用android:layout_toRightOf 标签之类的东西将按钮“放置”在旁边、顶部或其他任何地方。滚动视图必须是父视图,因为它只能接受一个子视图,在您的情况下,它应该是一个 LinearLayout(如果您要正确放置对象,则应该是一个相对的)。但是,您的 xml 的主要问题是 RelativeLayout。

【讨论】:

    【解决方案2】:

    使用RelativeLayout,您应该使用ALIGN_LEFT、ALIGN_RIGHT、LEFT_OF 等以避免这种重叠。其他方法是简单地使用LinearLayout。

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多