Android 如何在SherlockFragmentActivity类中设置actionbarsherlock的CustomView
我使用SherlockFragmentActivity作为我的主要活动课程。下面是代码Android 如何在SherlockFragmentActivity类中设置actionbarsherlock的CustomView,android,actionbarsherlock,android-actionbar,Android,Actionbarsherlock,Android Actionbar,我使用SherlockFragmentActivity作为我的主要活动课程。下面是代码 public class MainActivity extends SherlockFragmentActivity{ protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
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>
我已在清单文件中定义了我的活动,如下所示
<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 mView=getLayoutInflater().inflate(R.layout.YOUR_LAYOUT, null);
ActionBar ab=getSupportActionBar();
ab.setDisplayShowCustomEnabled(true);
ab.setCustomView(mView);
用这个
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.your_layout);
请详细说明你所说的“不能”是什么意思。。。?会发生什么?您期望发生什么?我的意思是显示默认操作栏而不是自定义布局操作栏。看起来您忘记启用显示自定义视图了。尝试添加
ab.setDisplayShowCustomEnabled(true)
。请发布您的答案,而不是评论。我会把它标为最佳答案。很高兴听到!@kettu也提出了同样的建议,所以请随意勾选他的答案。这在Android 2.3和4+上对我有效,但不是公认的答案。不过,被接受的答案在Android 4+上确实很有效。注意,此解决方案不会提供主页按钮——如果需要,请使用getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_home)代码>