代码之家  ›  专栏  ›  技术社区  ›  Lior Iluz

Android RelativeView-按钮消失

  •  0
  • Lior Iluz  · 技术社区  · 15 年前

    我一直在尝试创建一个类似于Android Facebook应用程序的布局(顶部的栏中有应用程序名和两个向右对齐的按钮)。

    什么都试过了:(离我最近的一个按钮消失了。。。

    下面是代码:(使用顶部的亲戚创建粘性页眉和页脚)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:orientation="vertical">
     <RelativeLayout android:layout_width="fill_parent"
      android:layout_height="50dip" android:id="@+id/top_control_bar">
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
       android:layout_height="fill_parent" android:src="@drawable/top_background">
       <TextView android:layout_width="wrap_content" android:layout_height="20dip" 
        android:text="APP" android:layout_gravity="left|center" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/add_bookmark" />
       <Button android:id="@+id/add_bookmark" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:text="Add" android:layout_gravity="right|center" android:layout_alignParentRight="true" />
       <Button android:id="@+id/search_bookmark" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:text="Search" android:layout_gravity="right|center" android:layout_alignParentRight="true"  />
      </RelativeLayout>
     </RelativeLayout>
     <LinearLayout android:id="@+id/bottom_control_bar"
      android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:layout_alignParentBottom="true">
      <Button android:layout_width="wrap_content"
       android:layout_height="fill_parent" android:text="Backup" />
      <Button android:layout_width="wrap_content"
       android:layout_height="fill_parent" android:text="Restore" />
     </LinearLayout>
     <ListView android:id="@android:id/list" android:layout_width="fill_parent"
      android:layout_height="0dip" android:layout_below="@id/top_control_bar"
      android:layout_above="@id/bottom_control_bar"></ListView>
     <TextView android:id="@android:id/empty" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="No Bookmarks found. You can add one by clicking the star."
      android:layout_below="@id/top_control_bar" android:layout_above="@id/bottom_control_bar" />
    </RelativeLayout>
    

    如果有任何帮助,我将不胜感激!:)

    2 回复  |  直到 15 年前
        1
  •  0
  •   hacksteak25    15 年前

    按钮在那里,但在搜索按钮后面。如果您切换add和search按钮的顺序,您将看到这两个按钮都在那里。两者共享相同的右边框坐标。使用 layout_toLeftOf layout_toRightOf 就像你对“应用程序”文本视图所做的那样。

    你可以替换你的内部 RelativeLayout

    
    <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
       <TextView android:layout_width="wrap_content" android:layout_height="20dip" 
        android:text="APP" android:layout_gravity="left|center" android:layout_alignParentLeft="true" android:layout_toLeftOf="@id/add_bookmark" />
       <Button android:id="@+id/search_bookmark" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:text="Search" android:layout_gravity="right|center" android:layout_alignParentRight="true" />
       <Button android:id="@+id/add_bookmark" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:text="Add" android:layout_gravity="right|center" android:layout_toLeftOf="@id/search_bookmark" />
    </RelativeLayout>
    
        2
  •  0
  •   Sameer Segal    15 年前

    它不会消失,它在你的搜索按钮下。这是因为两个按钮都是alignParentRight=true。

    将Search设置为parentRight,并将Add设置为Search的左侧。这应该能解决问题。

    另外:嵌套布局太多。你可以很容易地降低成本以提高效率。请记住,在android中创建/扩展视图非常昂贵(内存和时间)。