自定义了一个布局用来取代系统默认的ActionBar布局
后来,发现不能用这种方式获取ActionBar布局里组件的view,应该改为
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<Spinner
android:layout_centerVertical="true"
android:id="@+id/location"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<RadioGroup
android:id="@+id/mid"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/location"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="horizontal"
>
<RadioButton
android:button="@null"
android:background="@drawable/actionbar_mid_r_bg"
android:id="@+id/btn_left"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="@string/mid_left_btn"
android:textColor="@color/black"
android:padding="5dp"
/>
<RadioButton
android:button="@null"
android:background="@drawable/actionbar_mid_r_bg"
android:id="@+id/btn_right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="@string/mid_right_btn"
android:textColor="@color/black"
android:padding="5dp"
/>
</RadioGroup>
<CheckBox
android:id="@+id/actionbar_check"
android:layout_centerVertical="true"
android:button="@null"
android:background="@drawable/actionbar_right_check"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</RelativeLayout>
然后,我尝试给这个CheckBox添加一个OnCheckedChangeListener,但是却不能进入事件监听。
LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.film_actionbar,null);
checkbox=(CheckBox)view.findViewById(R.id.actionbar_check);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.v("@_@",""+isChecked); }else {
Log.v("@_@",""+isChecked); }
}
});
后来,发现不能用这种方式获取ActionBar布局里组件的view,应该改为
View view=getActionBar().getCustomView();
checkbox=(CheckBox)view.findViewById(R.id.actionbar_check);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.v("@_@",""+isChecked); }else {
Log.v("@_@",""+isChecked); }
}
});