[英]Center position of logo in Action Bar Android
I want to display the logo at the centre of the Action Bar. 我想在Action Bar的中心显示徽标。 Here's my custom layout code:
这是我的自定义布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ActionBarWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_centerInParent="true"
android:padding="8dp"/>
</RelativeLayout>
And in the onCreate()
I'm using it as: 在
onCreate()
我用它作为:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_logo);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2A2A2A")));
The issue is it displays fine in normal Fragments and Activity but shifts a little to the right when used with Navigation Drawer. 问题是它在正常的碎片和活动中显示正常,但在与导航抽屉一起使用时向右移动一点。 Here are the images:
这是图像:
What am I doing wrong? 我究竟做错了什么? Thanks.
谢谢。
Then try ToolBar. 然后尝试ToolBar。 Try this code
试试这个代码
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:src="@drawable/logo"
android:layout_gravity="center"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
Try using Linear layout and layout_gravity
like this : 尝试使用线性布局和
layout_gravity
如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ActionBarWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_gravity="center"
android:padding="8dp"/>
</LinearLayout>
hope it help 希望它有所帮助
将app:contentInsetLeft="0dp"
添加到android.support.v7.widget.Toolbar
,因为带有菜单抽屉的工具栏将自动具有右边距。
I used a Drawable as background of the Action Bar. 我使用Drawable作为Action Bar的背景。 In this way you can use different MenuItem on left and/or right and the logo will be everytime centered.
通过这种方式,您可以在左侧和/或右侧使用不同的MenuItem,徽标将每次居中。 I needed to animate also the logo when it has been loaded through Picasso, and this is the result:
我需要在通过毕加索加载徽标时制作动画,这就是结果:
I've used a LayerDrawable for animating just one Drawable (a layer) into it. 我使用了一个LayerDrawable来为它绘制一个Drawable(一层)动画。 This is the code, maybe could be useful for someone:
这是代码,可能对某些人有用:
getSupportActionBar().show();
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
showActionBarLogo(this, true);
And this is the showActionBarLogo function for showing and hiding the Action bar logo: 这是showActionBarLogo函数,用于显示和隐藏Action bar徽标:
public void showActionBarLogo(final Activity activity, boolean show)
{
if (show)
{
// Calculate Action bar height
int actionBarHeight = 200;
TypedValue tv = new TypedValue();
if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,activity.getResources().getDisplayMetrics());
}
// Using action bar background drawable
logoTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable[] layers = new Drawable[2];
layers[0] = new ColorDrawable(Color.RED); // Background color of Action bar
BitmapDrawable bd = new BitmapDrawable(activity.getResources(), bitmap);
bd.setGravity(Gravity.CENTER);
Drawable drawLogo = bd;
layers[1] = drawLogo; // Bitmap logo of Action bar (loaded from Picasso)
LayerDrawable layerDrawable = new LayerDrawable(layers);
layers[1].setAlpha(0);
((AppCompatActivity) activity).getSupportActionBar().setBackgroundDrawable(layerDrawable);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(layers[1], PropertyValuesHolder.ofInt("alpha", 255));
animator.setTarget(layers[1]);
animator.setDuration(2000);
animator.start();
}
@Override public void onBitmapFailed(Drawable errorDrawable) { }
@Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
};
Picasso.with(activity).load("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png").resize(0, actionBarHeight).into(logoTarget);
} else {
((AppCompatActivity) activity).getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));
}
}
I've created a drawable for the Action Bar with: 我为Action Bar创建了一个drawable:
I load the logo with Picasso and I like to animate it when has been loaded (bitmap onBitmapLoaded callback). 我用Picasso加载徽标,我喜欢在加载时为它设置动画(位图onBitmapLoaded回调)。
Bear in mind to declare Picasso Target out of showActionBarLogo function to avoid garbage issue on loading images: 请记住,在showActionBarLogo函数中声明Picasso Target,以避免加载图像时出现垃圾问题:
private Target logoTarget;
If you don't need the Picasso loading, you can use only the code into onBitmapLoaded
function and using a Drawable instead of the bitmap variable. 如果您不需要加载Picasso,则只能将代码用于
onBitmapLoaded
函数并使用Drawable而不是位图变量。
Same thing if you don't need the fade animation. 如果你不需要淡入淡出动画也一样。 You can remove the
layers[1].setAlpha(0);
你可以删除
layers[1].setAlpha(0);
line and also all the ObjectAnimator
lines. line以及所有
ObjectAnimator
行。
I've tested it from Android 4.4 (api 19) to 8.1 (api 27) and it works great! 我已经从Android 4.4(api 19)到8.1(api 27)进行了测试,效果很好!
I hope this could help! 我希望这可以帮助你!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ActionBarWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="clip_horizontal" // change from center attribute
android:contentDescription="@string/logo"
android:padding="2dp"
android:src="@drawable/tap1" />
</LinearLayout>
Using the attribute clip_horizontal did the trick for me. 使用属性clip_horizontal为我做了诀窍。
android:layout_gravity="clip_horizontal"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.