gpt4 book ai didi

android - CardView 工具栏

转载 作者:太空狗 更新时间:2023-10-29 16:33:14 30 4
gpt4 key购买 nike

我有一个包含 CardView 的 RecyclerView。

我想为每个 CardView 添加一个工具栏,使其看起来和行为都像主工具栏:

[图标] [标题] ......... [按钮] [按钮] [菜单]

我从此处 (http://blog.grafixartist.com/create-a-card-toolbar/) 看到可以在 CardView 中设置实际的 android.support.v7.widget.Toolbar 对象。但它依赖于 setSupportActionBar(...) 来扩充菜单并响应操作。

您认为可以在我的每个 CardView 中重现该行为吗?

最佳答案

您不需要为带有工具栏的卡片 View 设置 setSupportActionBar,除非您需要设置一些特定于 ActionBar 的向上/向后导航操作。

看看这个很好的例子(链接到下面的整个页面):

Thanks Gabriele for all the help. Here is working code:

Activity :

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mai);

Toolbar toolbar = (Toolbar) findViewById(R.id.card_toolbar);
toolbar.setTitle("Card Toolbar");
if (toolbar != null) {
// inflate your menu
toolbar.inflateMenu(R.menu.main);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return true;
}
});
}

Toolbar maintoolbar = (Toolbar) findViewById(R.id.toolbar_main);
if (toolbar != null) {
// inflate your menu
maintoolbar.inflateMenu(R.menu.main);
maintoolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return true;
}
});
}
}

}

Layout XML File:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_size_x2"
android:background="@android:color/holo_orange_dark"
android:minHeight="?attr/actionBarSize" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar_main"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="@dimen/action_bar_size"
android:orientation="vertical" >

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<android.support.v7.widget.Toolbar
android:id="@+id/card_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_size"
android:background="@android:color/white" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/app_name"
android:textSize="24sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

</RelativeLayout>

Make sure your activity theme is extending Theme.AppCompat.Light.NoActionBar.

Here is what it will look like:

enter image description here

Few things to note:

  • If you are using card elevation then you need to altar the margin top so that your card aligns with the main toolbar
  • I am still seeing 1-2 pixel margin between bottom of main toolbar and card toolbar. Not sure about what to do in this case. For now, iam aligning it manually.

From: How to create a card toolbar using appcompat v7

请注意,这篇文章的作者根本没有使用过它,所以我很确定您也不需要它来实现您的想法

希望对你有帮助

关于android - CardView 工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34622621/

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