在android中创建自己的动作栏
Posted
技术标签:
【中文标题】在android中创建自己的动作栏【英文标题】:Create own action bar in android 【发布时间】:2012-10-17 12:03:35 【问题描述】:我在 android 中的操作栏有一些问题。我想在左侧和右侧有一些图标。在左侧我想要 2 个图标和文本,在右侧我想要 6 个图标,但是当文本很长时,我想在右侧隐藏一些图标。在标准操作栏中,我只能从右到左添加项目,所以我决定创建自己的操作栏。我该如何开始?我需要知道什么才能做到这一点。你能给我一些建议或例子吗?
【问题讨论】:
【参考方案1】:它非常简单。创建将从某些布局扩展的新类。将具有图标和按钮的 xml 膨胀。并为逻辑显示\隐藏\设置侦听器等方法。我很早就做了,但现在我可以说这不是一个好主意。有一些指导方针,创建应用程序非常重要。
来自我的旧项目的示例:
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_
android:layout_
android:background="@drawable/avtion_bar_bg" >
<ToggleButton
android:id="@+id/actionbar_togglebutton_back"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<ToggleButton
android:id="@+id/actionbar_togglebutton_save"
android:layout_
android:layout_
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_
android:background="@color/white25transpert"
android:layout_toLeftOf="@id/actionbar_togglebutton_save"
android:layout_
android:layout_centerInParent="true">
</FrameLayout>
<ToggleButton
android:id="@+id/actionbar_togglebutton_clear"
android:layout_
android:layout_
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_
android:layout_
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>
祝你好运!
【讨论】:
以上是关于在android中创建自己的动作栏的主要内容,如果未能解决你的问题,请参考以下文章