[英]Display icon and title in the ActionBar in Android
我想在活動的操作欄中顯示圖標和標題。 這是我的應用程序的屏幕截圖:
現在我需要在操作欄中的MyApp文本左側顯示應用程序徽標。 我已經嘗試了以下代碼來設置徽標但沒有發生任何事情:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setLogo(R.drawable.ic_launcher_web);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.activity_main);
}
我剛試過你的代碼,這很有效。
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
確保您的父主題設置為Theme.AppCompat.Light.NoActionBar
。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
您可以為操作欄創建新布局,並且可以在主活動中實現
創建新布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:minHeight="60dp"
android:id="@+id/topHeaderNav"
android:background="@color/primary"
android:layout_height="60dp"
tools:showIn="@layout/activity_main">
<ImageView
android:layout_width="50dp"
android:maxHeight="75dp"
android:maxWidth="75dp"
android:id="@+id/imgTop"
android:layout_centerVertical="true"
android:src="@drawable/mag"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/topTitleText"
android:layout_toRightOf="@id/imgTop"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="Magnetic Orange"
android:textColor="#FFFFFF"
/>
並在主布局中實現它
`<include layout="@layout/navbar"
android:id="@+id/topHeaderNavMainActivity"
/>`
我認為它會對你有所幫助
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
所有的拳頭都要確保你使用了NoActionBar
風格
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
在onCreate()
設置工具欄並確保它是V7
Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolbar);
xml中的工具欄看起來像
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"/>
並使用您之前嘗試過的那些方法。 這會奏效。
如果您需要自定義操作欄然后在操作欄中使用服裝布局,以下是解釋如何自定義操作欄的代碼,
在您的活動中將此代碼放在了創建方法上
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.actionbar_layout, null); //hear set your costume layout
getSupportActionBar().setCustomView(v);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8731a0")));
它是活動繼承AppCompatActivity的代碼,或者如果您繼承活動,則將getSupportActionBar()替換為getActionBar()並使用。
private ViewGroup actionBarLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
context = this;
actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.custom_actionbar, null);
actionBarLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// Set up your ActionBar
/* final ActionBar actionBar = getActionBar();*/
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(actionBarLayout);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.green)));
init();
event();
}
and used following style.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/green</item>
<item name="colorPrimaryDark">@color/green</item>
</style>
嘗試自定義ActionBar。
custom_actionbar.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:clickable="true"
android:background="#ffffff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="1">
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginLeft="15sp"
android:background="@drawable/emaact"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:id="@+id/imageView"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="right">
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Project"
android:layout_gravity="center_vertical"
android:textSize="20sp"
android:id="@+id/textView" android:gravity="center_vertical"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.99"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txttitle"
android:text="title"
android:textStyle="normal"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="1.06"/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/menuline"
android:layout_margin="10dp"
android:id="@+id/slidingmenu"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</RelativeLayout>
在您的MainActivity中,使用此代碼。
public void customBar(){
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.custom_actionbar, null);
TextView text =(TextView) customView.findViewById(R.id.txttitle);
text.setText("Home");
ImageView menus =(ImageView) customView.findViewById(R.id.slidingmenu);
actionBar.setCustomView(customView);
actionBar.setDisplayShowCustomEnabled(true);
}
您的操作欄看起來像... 自定義ActionBar
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.