android - 如何在 SherlockFragmentActivity 类中设置 actionbarsherlock 的 CustomView

标签 android actionbarsherlock android-actionbar

我正在使用 SherlockFragmentActivity 作为我的主要 Activity 类。下面是代码...

 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);}  }

我也试过

getSupportActionBar().setCustomView(customView);

这是我的 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>

我在 list 文件中定义了我的 Activity 如下

<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>

现在我的问题是我无法设置我的 actiobar 的自定义 View 。谁能帮我。 提前致谢。

最佳答案

试试这个

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

关于android - 如何在 SherlockFragmentActivity 类中设置 actionbarsherlock 的 CustomView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18224688/

相关文章:

java - Jsoup 在指定标签后开始解析还是从页面底部开始?

android - 以编程方式自定义 Android Wear watch 超时

android - 每个带有 ActionBarSherlock 的选项卡 fragment 的操作栏菜单

android - 使用自定义操作栏/工具栏 View 创建选项菜单

android - 我怎么知道是否提供了 Geocoder android?

android - RxJava - 如何在取消订阅 observable 后继续后台任务

android - 如何以编程方式设置操作栏上的文本

android - 更改选项卡标题的字体 - ActionBarSherlock

android - 如何使选项卡(ActionBar)停止调用其邻居选项卡的 onCreateView()

android - 在 Android 中禁用操作栏按钮