gpt4 book ai didi

android - 在android中创建自己的操作栏

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:26 26 4
gpt4 key购买 nike

我对 android 中的操作栏有一些问题。我想在左侧和右侧有一些图标。在左侧我想有 2 个图标和文本,在右侧我想有 6 个图标,但是当文本很长时我想在右侧隐藏一些图标。在标准操作栏中,我只能从右到左添加项目,所以我决定创建自己的操作栏。我该如何开始?我需要知道什么才能做到这一点。你能给我一些建议或例子吗?

最佳答案

非常简单。创建将从某些布局扩展的新类。膨胀将具有图标和按钮的 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_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>

祝你好运!

关于android - 在android中创建自己的操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12933905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com