我在android中的操作栏有一些问题。我想在左侧和右侧有一些图标。在左侧我希望有2个图标和文字,在右侧我想有6个图标,但是当文字很长时我想在右侧隐藏一些图标。在标准操作栏中,我只能从右到左添加项目,因此我决定创建自己的操作栏。我怎么开始?我需要知道这样做。你能给我一些建议或例子吗?
答案 0 :(得分:6)
非常简单。创建将从某些布局扩展的新类。给xml充气,它将有图标和按钮。并为逻辑show \ hide \ set listeners等制作方法。我早做了,但现在我可以说这不是一个好主意。有必要的创建应用程序的准则和非常重要。
我旧项目的例子:
public class ActionBar extends RelativeLayout {
private ToggleButton buttonSave;
private ToggleButton buttonClear;
private ToggleButton buttonHelp;
private ToggleButton buttonBack;
private FrameLayout sepparator;
private TextView textview;
private ImageView imageViewIcon;
public ActionBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.actionbar, this);
initView();
}
private void initView () {
buttonSave = (ToggleButton) findViewById(R.id.actionbar_togglebutton_save);
buttonBack = (ToggleButton) findViewById(R.id.actionbar_togglebutton_back);
buttonHelp = (ToggleButton) findViewById(R.id.actionbar_togglebutton_help);
textview = (TextView) findViewById(R.id.actionbar_textview);
imageViewIcon = (ImageView) findViewById(R.id.actionbar_imageview_icon);
buttonClear = (ToggleButton) findViewById(R.id.actionbar_togglebutton_clear);
sepparator = (FrameLayout) findViewById(R.id.actionbar_sepparator);
}
public void setOnSaveButtonClickListener (OnClickListener listener) {
buttonSave.setOnClickListener(listener);
}
public void setSaveButtonDrawable (Drawable d) {
buttonSave.setBackgroundDrawable(d);
buttonSave.setVisibility(View.VISIBLE);
}
public void setOnHelpButtonClickListener (OnClickListener listener) {
buttonHelp.setOnClickListener(listener);
}
public void setHelpButtonDrawable (Drawable d) {
buttonHelp.setBackgroundDrawable(d);
buttonHelp.setVisibility(View.VISIBLE);
}
public void setOnBackButtonClickListener (OnClickListener listener) {
buttonBack.setOnClickListener(listener);
}
public void setBackButtonDrawable (Drawable d) {
buttonBack.setBackgroundDrawable(d);
buttonBack.setVisibility(View.VISIBLE);
}
public void setClearButtonDrawable (Drawable d) {
buttonClear.setBackgroundDrawable(d);
buttonClear.setVisibility(View.VISIBLE);
}
public void setOnClearButtonClickListener (OnClickListener listener) {
buttonClear.setOnClickListener(listener);
}
public void setTitle (String value) {
textview.setText(value);
}
public void setIcon (int resId) {
imageViewIcon.setImageDrawable(getResources().getDrawable(resId));
}
public void setOnIconClickListener (OnClickListener listener) {
imageViewIcon.setOnClickListener(listener);
}
public void setSaveButtonChecked (boolean value) {
buttonSave.setChecked(value);
}
public void setSeporatorVisible (int visibility) {
sepparator.setVisibility(visibility);
}
public void setOnIconListener (OnClickListener listener) {
imageViewIcon.setOnClickListener(listener);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/avtion_bar_bg" >
<ToggleButton
android:id="@+id/actionbar_togglebutton_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:visibility="gone"
android:background="@drawable/arrow_back_button_drawable"
android:textOn=""
android:textOff="" />
<ImageView
android:id="@+id/actionbar_imageview_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/actionbar_togglebutton_back"
android:layout_marginLeft="19.1dp"/>
<TextView
android:id="@+id/actionbar_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="@color/actionbar_text"
android:textSize="16dp"
android:layout_toRightOf="@id/actionbar_imageview_icon"
android:layout_marginLeft="56.1dp" />
<RelativeLayout
android:id="@+id/actionbar_relativelayout_saveconteiner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<ToggleButton
android:id="@+id/actionbar_togglebutton_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/save_btn_drawable"
android:textOn=""
android:textOff=""/>
<FrameLayout
android:id="@+id/actionbar_sepparator"
android:layout_width="0.5dp"
android:background="@color/white25transpert"
android:layout_toLeftOf="@id/actionbar_togglebutton_save"
android:layout_height="35dp"
android:layout_centerInParent="true">
</FrameLayout>
<ToggleButton
android:id="@+id/actionbar_togglebutton_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_toLeftOf="@id/actionbar_sepparator"
android:layout_marginRight="4dp"
android:layout_centerVertical="true"
android:background="@drawable/clear_button_drawable"
android:textOn=""
android:textOff=""/>
</RelativeLayout>
<ToggleButton
android:id="@+id/actionbar_togglebutton_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/save_btn_drawable"
android:layout_marginRight="10dp"
android:textOn=""
android:textOff=""/>
</RelativeLayout>
祝你好运!