gpt4 book ai didi

android - 如何在 ActionBar 中显示自定义 View ?

转载 作者:IT老高 更新时间:2023-10-28 13:07:00 26 4
gpt4 key购买 nike

我想在操作栏中显示自定义搜索(我正在使用 ActionBarSherlock)。

我明白了:

enter image description here

但我想让自定义布局(编辑文本字段)占据整个可用宽度。

我已经按照建议实现了自定义布局 here .

有我的自定义布局search.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:focusable="true" >

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|fill_horizontal" >

<EditText
android:id="@+id/search_query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="@drawable/bg_search_edit_text"
android:imeOptions="actionSearch"
android:inputType="text" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:src="@drawable/ic_search_arrow" />

</FrameLayout>

</LinearLayout>

MyActivity中:

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.drawable.ic_action_search);

LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);

actionBar.setCustomView(v);

如何让自定义布局占据actionBar的所有可用宽度?

请帮忙。

最佳答案

这有一个技巧。您所要做的就是使用 RelativeLayout 而不是 LinearLayout 作为主容器。为它设置 android:layout_gravity="fill_horizo​​ntal" 很重要。应该这样做。

关于android - 如何在 ActionBar 中显示自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12883732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com