我正在开发一个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);
}
答案 0 :(得分:0)
##试试这段代码##
创建布局文件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);
}