Android如何在线性布局中添加ActionBar?

问题描述:

如何在RelativeLayout中添加一个操作栏?我正在使用actionbarSherlock添加一个操作栏,但我无法弄清楚如何将它添加到线性布局中。原因是我想在操作栏上方和下方添加一些元素。Android如何在线性布局中添加ActionBar?

编辑:

使用this library我可以用另一种观点认为向上滑动从底部的面板。但问题在于操作栏保持在顶部。下面是代码,以使2片在layout.xml

<com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/sliding_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="bottom" 
     sothree:panelHeight="68dp" 
     sothree:shadowHeight="4dp"> 

<RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:id="@+id/relativeone"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="Main Content" 
      android:textSize="16sp" /> 
</RelativeLayout> 


<RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:id="@+id/relativtwo"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center|top" 
      android:text="The Awesome Sliding Up Panel" 
      android:textSize="16sp" /> 

</RelativeLayout> 

    </com.sothree.slidinguppanel.SlidingUpPanelLayout> 

这是我试过,但没有工作仍然

 <com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/sliding_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="bottom" 
      sothree:panelHeight="68dp" 
      sothree:shadowHeight="4dp"> 

    <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:id="@+id/relativeone"> 

//I tried to place the menu in here but its completely wrong 
    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> 
    <item android:id="@+id/action_settings" android:title="@string/action_settings" 
     android:orderInCategory="100" /> 
</menu> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:text="Main Content" 
       android:textSize="16sp" /> 
    </RelativeLayout> 


    <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:id="@+id/relativtwo"> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center|top" 
       android:text="The Awesome Sliding Up Panel" 
       android:textSize="16sp" /> 

    </RelativeLayout> 

     </com.sothree.slidinguppanel.SlidingUpPanelLayout> 
+0

操作栏应该是您应用中最顶层的元素。被说不,你不能,除非你创建自己的视图 – tyczj 2014-11-06 15:09:55

可以显示什么是你的工作吗?这几乎类似于Custom action bar in relative layout

+0

我想让这个库附加一个操作栏到其中一个视图https://github.com/umano/AndroidSlidingUpPanel – 2014-11-06 15:43:07

+1

好的库!我只能想,如果现在是,你可以创建多个视图/布局的层次结构来放置元素。如github示例中所述,您的主滑动面板布局需要两个子组件。[自定义组件](http:// developer。 android.com/guide/topics/ui/custom-components.html)。这可能也有帮助。 – Chetandalal 2014-11-06 15:57:40