如何在SherlockFragmentActivity类中设置actionbarsherlock的自定义视图

Raj*_*Raj 2 android actionbarsherlock android-actionbar

我使用SherlockFragmentActivity作为我的主要活动类.下面是代码......

 public class MainActivity extends SherlockFragmentActivity{
  protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar ab = getSherlock().getActionBar();
    LayoutInflater li = LayoutInflater.from(this);
    View customView = li.inflate(R.layout.my_custom_layout, null);
    customView.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT ,ActionBar.LayoutParams.MATCH_PARENT));
    ab.setCustomView(customView);
    ab.setDisplayShowHomeEnabled(false);}  }
Run Code Online (Sandbox Code Playgroud)

我也尝试过

getSupportActionBar().setCustomView(customView);
Run Code Online (Sandbox Code Playgroud)

这是我的my_custom_layout.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="fill_parent"
android:background="@drawable/light_gradient" >

<Button
    android:id="@+id/btn_actionbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:textSize="15sp"
    android:text="Settings" />

<TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="20sp"
    android:textColor="@color/White"
    android:text="Home"/></RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我在清单文件中定义了我的活动,如下所示

<activity
        android:name="com.example.demo.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.Sherlock.Light" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="com.actionbarsherlock.sample.demos.EXAMPLE" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

现在我的问题是我无法设置我的actiobar的自定义视图.谁能帮我.Thanx提前.

Ket*_*hir 6

试试这个

 View mView=getLayoutInflater().inflate(R.layout.YOUR_LAYOUT, null);
 ActionBar ab=getSupportActionBar();
 ab.setDisplayShowCustomEnabled(true);
 ab.setCustomView(mView);
Run Code Online (Sandbox Code Playgroud)