2016-02-12 149 views
2

我知道你可以将图标图像添加到应用程序栏,但是我想知道是否有方法在Android上的应用程序栏中放置较大的徽标图像。附件中的图片将其放在应用程序栏后面,但我试图在应用程序栏中找到它。这可能吗?如何在Android应用程序栏中添加徽标图像?

enter image description here

+0

http://stackoverflow.com/ a/16029214/2826147 –

回答

0

至于你的形象是大,长方形,你可以将它设置在你的动作条的背景:

final ActionBar actionBar = getActionBar(); 
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.raw.actionbar_background)); 
actionBar.setBackgroundDrawable(background); 

为此,您可以创建一个activity说“BaseActivity”和onCreate添加片段方法,并在您想要显示您的更改的所有活动中扩展该活动 或者您可以将不同的方形徽标设置为应用图标并将其设置为您的操作栏图标。

public BaseActivity extends Activity{ 
@override 
public void onCreate(Bundle bundle){ 
/// add snippet 
} 
} 

yourActivity/yourActivities extends BaseActivity 
+0

这段代码在哪里?每个活动的java文件? – Casey

+0

看到我更新的答案@Casey – KDeogharkar

1

1)使用最新的Android工具栏

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

<!-- Place your ImageView and Image--> 
<!-- mention image size--> 
</android.support.v7.widget.Toolbar> 

示例代码:

<android.support.v7.widget.Toolbar 
android:id="@+id/toolbar" 
android:layout_width="match_parent" 
android:layout_height="?attr/actionBarSize" 
android:background="?attr/colorPrimary" 
app:layout_scrollFlags="scroll|enterAlways" 
app:popupTheme="@style/AppTheme" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
> 

<LinearLayout 
    android:id="@+id/back_menu_ll" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="center"> 
    <ImageView 
     android:id="@+id/logo" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:src="@drawable/ic_logo" 
     /> 
    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Title" 
     android:textColor="@color/title_first_textcolor" 
     android:textSize="@dimen/abc_text_size_title_material" 
     android:gravity="left" 
     /> 
    </android.support.v7.widget.Toolbar> 

2)您可以通过编程设置标志:

Toolbar tool_bar = (Toolbar) findViewById(R.id.app_bar); 
setSupportActionBar(tool_bar); 
tool_bar.setLogo(R.drawable.image); 

有关工具栏的详细信息,检查这个链接: http://developer.android.com/reference/android/widget/Toolbar.html

+0

此代码是否在app_bar_home_page.xml文件或其他位置?我得到一个错误:错误解析XML:在这一行不匹配的标记“” – Casey

+0

在Gradale文件中添加Appcompat Libary –

0

您需要添加的ImageView工具栏

+0

我试过这个,它给了我一个错误,说它必须是以# – Casey

+0

开头的颜色值可以粘贴代码吗? – Rohit

2

有关最新API的XML应该是内部的,

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

活动是应该的,

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_layout); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
    toolbar.setLogo(R.drawable.ic_call_black_24dp); 
    setSupportActionBar(toolbar); 
} 
+0

试过这个,并得到一个错误:java.lang.RuntimeException:无法启动活动ComponentInfo {edu.csusb.ossmassistant/edu.csusb.ossmassistant.home_page}:java.lang.IllegalStateException:已附加 – Casey

相关问题