gpt4 book ai didi

android - 如何在工具栏中添加图像

转载 作者:IT老高 更新时间:2023-10-28 23:39:22 25 4
gpt4 key购买 nike

我关注 this创建我的应用程序的示例现在我正在尝试在我的工具栏中添加图像但图像未显示,我正在尝试设置我的应用程序 Logo ,以下是我的代码和工具栏 xml,任何人都可以告诉我什么是错误?

工具栏.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/MyMaterialTheme.Base"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toolbartitle"
/>

</android.support.v7.widget.Toolbar>

MainActivity.java

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

private static String TAG = MainActivity.class.getSimpleName();

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
private String chng;
private Intent i;
public SearchView searchView;
public SearchManager searchManager;

// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();

// Session Manager Class
SessionManager session;

private String id;
private boolean b;
private String rasa;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
session = new SessionManager(getApplicationContext());
id = getIntent().getStringExtra("id");
System.out.println("MAIN ID : " + id);
/* i=getIntent();
chng=i.getStringExtra("Changes");*/

// Toast.makeText(getApplicationContext(),chng,Toast.LENGTH_LONG).show();
mToolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);

drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawerLayout),mToolbar);
drawerFragment.setDrawerListener(this);
displayView(0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

这就是我的工具栏现在的显示方式

enter image description here

最佳答案

  <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/color_blue"
app:popupTheme="@style/AppTheme.PopupOverlay">

<ImageView
android:id="@+id/tv_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/header_title" />
</android.support.v7.widget.Toolbar>

你也可以通过这个方法设置按钮的事件...

 public static void showToolBar(Toolbar toolbar,
final AppCompatActivity activity) {
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
@SuppressWarnings("deprecation")
Drawable drawable = activity.getResources().getDrawable(
R.mipmap.back_icon);
drawable.setColorFilter(
activity.getResources().getColor(R.color.color_white),
android.graphics.PorterDuff.Mode.SRC_ATOP);
activity.getSupportActionBar().setHomeAsUpIndicator(drawable);
toolbar.setBackgroundColor(activity.getResources().getColor(
R.color.color_blue));
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activity.finish();
}
});
}

关于android - 如何在工具栏中添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33300978/

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