自定义ActionBar和RoboGuice注入

时间:2014-03-14 13:56:07

标签: android roboguice

我为我的项目创建了一个自定义ActionBar,我想使用(InjectView)为ActionBar注入视图元素。但是,这不起作用。另一方面,findViewById工作正常。

我认为这个问题来自ActionBar是在MainActivity完成布局之后创建的。但是,这也没有意义,因为我仍然可以使用findViewbyId来获取视图。

以下是操作栏的XML定义:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/activityTitleTxt"
            android:layout_alignParentTop="false"
            android:layout_gravity="center"
            android:textColor="@color/white" />

    </LinearLayout>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:showDividers="middle"
        android:id="@+id/filterRadioGroup"
        android:layout_toLeftOf="@+id/layoutRadioGroup">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="All"
            android:id="@+id/selectAllBtn"
            android:background="@null"
            android:button="@null"
            android:textColor="@color/white"
            android:layout_marginRight="5dp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@android:drawable/btn_star"
            android:id="@+id/selectFavBtn" />
    </RadioGroup>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:id="@+id/layoutRadioGroup"
        android:layout_toLeftOf="@+id/shareSearchLayout"
        android:gravity="center">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listViewBtn"
            android:background="@null"
            android:button="@drawable/ic_action_view_as_list"
            android:gravity="center"
            android:checked="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tileViewBtn"
            android:background="@null"
            android:button="@drawable/ic_action_picture"
            android:gravity="center" />
    </RadioGroup>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="0dp"
        android:id="@+id/shareSearchLayout">

        <SearchView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searchView"
            android:layout_gravity="center" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/shareBtn"
            android:background="@android:color/transparent"
            android:src="@android:drawable/ic_menu_share"
            android:layout_gravity="center" />

    </LinearLayout>

</RelativeLayout>

动作栏视图:

public class ActionBarView extends LinearLayout {

    private MainActivity activity;

    @InjectView(R.id.activityTitleTxt)
    private TextView activityTitleTxt;
    @InjectView(R.id.filterRadioGroup)
    private RadioGroup filterGroup;
    @InjectView(R.id.layoutRadioGroup)
    private RadioGroup layoutGroup;
    @InjectView(R.id.searchView)
    private SearchView searchView;
    @InjectView(R.id.shareBtn)
    private ImageButton shareBtn;

    public ActionBarView(MainActivity activity) {
        super(activity);

        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.action_bar_view, this);

//        this.activityTitleTxt = (TextView) findViewById(R.id.activityTitleTxt);
////        this.selectAllTxt = (TextView) findViewById(R.id.selectAllTxt);
////        this.selectFavBtn = (ImageButton) findViewById(R.id.selectFavBtn);
//        this.filterGroup = (RadioGroup) findViewById(R.id.filterRadioGroup);
//        this.layoutGroup = (RadioGroup) findViewById(R.id.layoutRadioGroup);
//        this.searchView = (SearchView) findViewById(R.id.searchView);
//        this.shareBtn = (ImageButton) findViewById(R.id.shareBtn);

        this.activity = activity;
        setListeners();
    }

    private void setListeners() {
        this.filterGroup.setOnCheckedChangeListener(activity);
        this.layoutGroup.setOnCheckedChangeListener(activity);
    }

    public void setActivityTitle(String title){
        this.activityTitleTxt.setText(title);
    }
}

以下是我在主要活动中附加ActionBar的方法:

public class MainActivity extends BaseActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{


@InjectView(R.id.quoteGridView)
private AbsListView absListView;
@InjectView(R.id.header1Txt)
private TextView header1Txt;
@InjectView(R.id.header2Txt)
private TextView header2Txt;
@InjectView(R.id.header3Txt)
private TextView header3Txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // ActionBar configurations
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);

    ActionBarView actionBarView = new ActionBarView(this);
    actionBarView.setActivityTitle("Quote");
    actionBar.setCustomView(actionBarView);

2 个答案:

答案 0 :(得分:1)

由于RoboGuice在setContentView(R.layout.activity_main)注入视图,我能够通过在setContentView(R.layout.activity_main)之前移动actionBar.setCustomView(actionBarView)来解决此问题。

答案 1 :(得分:0)

InjectView在您的情况下将视图附加到已定义的上下文(Activity),您无法将视图注入扩展LinearLayout的类,是的,它正在使用findViewbyId因此函数绑定view到已定义的上下文。