[英]How to Show Home Icon and Centered Text in actionbar
我正在開發一個android應用程序,我想在左側顯示圖標,並且標題居中。 當我嘗試以居中為標題時,活動圖標未顯示。 請告訴我解決方案。 我錯了。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.center_action_bar_text, null);
actionBar.setCustomView(v);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//actionBar.setHomeButtonEnabled(true);
//actionBar.setDisplayHomeAsUpEnabled(true);
/*actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.center_action_bar_text);*/
Tab tab = actionBar.newTab();
tab.setText("abc");
tab.setIcon(R.drawable.abc_ic_go);
tab.setTabListener(new TabListener() {
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
});
actionBar.addTab(tab);
Tab tab1 = actionBar.newTab();
tab1.setText("abc");
tab1.setIcon(R.drawable.abc_ic_search);
tab1.setTabListener(new TabListener() {
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
});
actionBar.addTab(tab1);
Tab tab2 = actionBar.newTab();
tab2.setText("abc");
tab2.setIcon(R.drawable.abc_ic_go);
tab2.setTabListener(new TabListener() {
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
});
actionBar.addTab(tab2);
}
##嘗試此代碼##
創建一個布局文件custom_actionbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp" >
<ImageView
android:id="@+id/app_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="@string/app_icon"
android:paddingEnd="5dp"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/app_name"
android:textColor="#fff"
android:textSize="17sp" />
</RelativeLayout>
還有你的java代碼
public class MainActivity extends ActionBarActivity {
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
// Custom actionbar
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, 50);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
LayoutInflater actionbarLayoutInflater = LayoutInflater.from(this);
View actionbarView = actionbarLayoutInflater.inflate(
R.layout.custom_actionbar, null);
TextView appName = (TextView) actionbarView
.findViewById(R.id.app_title);
ImageView appLogo = (ImageView) actionbarView
.findViewById(R.id.app_icon);
appName.setLayoutParams(lp);
appName.setGravity(Gravity.CENTER);
actionBar.setCustomView(actionbarView);
actionBar.setDisplayShowCustomEnabled(true);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.