gpt4 book ai didi

android - 如何扩展 ActionBar

转载 作者:行者123 更新时间:2023-11-30 02:05:52 25 4
gpt4 key购买 nike

当我尝试在 ActionBar 的正下方添加一个 View 时,它似乎只有在我添加一个 ImageView 时才有效,如下所示。

同样不适用于 LinearLayout 或 RelativeLayout,而是删除所有 ActionBar 的初始内容并将其替换为 Linear/RelativeLayout 内容。

为什么我不能成功添加 LinearLayout 或 RelativeLayout,或者我哪里出错了。我刚开始使用 Android。

我的目标是这样的: enter image description here

-- 代码

通过添加 ImageView 扩展我们的 ActionBar

    ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

ImageView imageView = new ImageView(actionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.drawable.center16);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
imageView.setLayoutParams(layoutParams);
actionBar.setCustomView(imageView);

使用线性或相对布局

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

ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
linearorrelative.setLayoutParams(layoutParams);
actionBar.setCustomView(linearorrelative);

最佳答案

如果您希望在 ActionBar 下方有一个搜索栏,您应该考虑使用新的 Toolbar .即:

a generalization of action bars for use within application layouts

这是一个使用新的 AppCompat library 的示例实现,根据您的图片:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />

<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp" />

</LinearLayout>

<!-- Your content here -->

</LinearLayout>

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

}

<style name="Your.Base.Theme" parent="Theme.AppCompat.NoActionBar">
...
</style>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"...>

<application
...
android:theme="@style/Your.Base.Theme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>

结果

results

否则,按照@ChrisStillwell 的建议,考虑使用 ActionView并将 SearchView 作为 MenuItem 放在 ActionBar 中。

关于android - 如何扩展 ActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30674733/

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