【问题标题】:How can i place the search bar at the bottom of layout in android?如何将搜索栏放置在 android 布局的底部?
【发布时间】:2015-05-07 05:29:20
【问题描述】:

我需要在活动底部放置搜索栏。如何在android中实现?

【问题讨论】:

  • 你可以使用相对布局,你的文本字段可以使用 alignparentbottom(true) 设置为底部
  • 最简单的方法是,如果您只想添加搜索功能而不是没有操作栏,那么您可以在布局底部添加编辑框,并可以编写从列表中搜索的逻辑
  • 你现在有什么?您是否在屏幕顶部使用ActionBar 实现了它??

标签: android android-layout android-search


【解决方案1】:

您可以在此链接中查看

http://forums.xamarin.com/discussion/8771/how-do-i-add-an-actionbar-to-the-bottom

Two action bars (Bottom and Up) at the same time?

在底部添加一个 ActionBar 并在其中放置搜索栏

<LinearLayout>
  <RelativeLayout>
     ... (Top bar content can go here)
  </RelativeLayout>
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
  <!--This is assuming you are going to use a listview, you can put anything here, just make sure to put the weight attribute into whatever you end up using-->
  <ListView
    android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:cacheColorHint="@color/black"
            android:layout_weight="1" />
  <!--Bottom bar layout below-->
  <LinearLayout
    android:orientation="horizontal"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout2"
            android:layout_weight="0" >
  ...Put your bottom bar content here...
  </LinearLayout>
 </LinearLayout>
</LinearLayout>

http://developer.android.com/guide/topics/ui/actionbar.html

添加操作栏 如上所述,本指南重点介绍如何使用支持库中的 ActionBar API。因此,在添加操作栏之前,您必须按照支持库设置中的说明使用 appcompat v7 支持库设置您的项目。

使用支持库设置项目后,以下是添加操作栏的方法:

通过扩展 ActionBarActivity 创建您的活动。 为您的活动使用(或扩展)其中一个 Theme.AppCompat 主题。例如:

<activity android:theme="@style/Theme.AppCompat.Light" ... >

添加操作项

res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"/>
    <item android:id="@+id/action_compose"
          android:icon="@drawable/ic_action_compose"
          android:title="@string/action_compose" />
</menu>

然后在您的活动的 onCreateOptionsMenu() 方法中,将菜单资源膨胀到给定的菜单中以将每个项目添加到操作栏:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

要请求将项目作为操作按钮直接显示在操作栏中,请在标记中包含 showAsAction="ifRoom"。例如:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>

如果操作栏中的项目没有足够的空间,它将出现在操作溢出中。

使用支持库中的 XML 属性 请注意,上面的 showAsAction 属性使用标签中定义的自定义命名空间。这在使用支持库定义的任何 XML 属性时是必需的,因为这些属性在旧设备上的 Android 框架中不存在。因此,您必须使用自己的命名空间作为支持库定义的所有属性的前缀。 如果您的菜单项同时提供标题和图标(带有标题和图标属性),则默认情况下操作项仅显示图标。如果要显示文本标题,请将“withText”添加到 showAsAction 属性。例如:

<item yourapp:showAsAction="ifRoom|withText" ... />

【讨论】:

    【解决方案2】:

    是的,您可以通过在应用程序中使用拆分操作栏... 这是您可以参考的链接 http://developer.android.com/guide/topics/ui/actionbar.html

    还有一个教程 这里

    http://android-er.blogspot.in/2012/06/split-action-bar-for-android-4.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多