可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm in search of a way to style the ActionBar's "up" icon that is displayed when you configured it to be displayed from your Activity :
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I've been looking around and found a way to add padding between the home icon and the title (Padding between ActionBar's home icon and title), but not a way to add padding between the home icon and the up indicator icon.
I dug into ActionBarView.HomeView
in the android source and it appears to inflate the up indicator via this call :
mUpView = findViewById(com.android.internal.R.id.up);
Problem is the com.android.internal.R.id.up
is internal so I can't access it, so the above linked solution for padding the home icon won't work. Ideally I'd like to override the ActionBarView.HomeView
, but it's private within the ActionBarView
.
Does anyone know if ActionBarSherlock provides a good way to override this behavior? I've looked into providing a custom home layout, but the threads I found indicated that it wouldn't be a good idea to diverge from the standard android classes.
Any ideas?
回答1:
As of API 15 the positioning of these elements are hard-coded into the layout resources. The only alternative you have would be to use a custom navigation layout to create your own and hide the built-in home navigation.
回答2:
This solution will work
ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(10, 0, 0, 0);
BUT, you'll be forced to put this code in a parent Activity, or in every activity you want to add the padding to the home button.
I suggest this approach:
Create a drawable layer-list with the following code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap
android:antialias="true"
android:gravity="center"
android:src="@drawable/btn_back_actionbar_normal" />
</item>
<item>
<shape android:shape="rectangle" >
<size android:width="24dp" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
</layer-list>
In your styles add:
For values-11:
<item name="android:homeAsUpIndicator">@drawable/actionbar_back_selector</item>
For values:
<item name="homeAsUpIndicator">@drawable/actionbar_back_selector</item>
This way you'll be adding a padding between the back button, and the Home button in the compat actionbar, and you'll have to add this code only once, and will be available in the whole app!
回答3:
Actually this answer is correct. Just set the left padding of the home button and it will add padding between the home button and the up arrow
ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(10, 0, 0, 0);
回答4:
Maybe it works if you add the padding to the graphic (in Photoshop, for example) and set this graphic on the theme with the attribute
<item name="android:homeAsUpIndicator">@drawable/up_with_padding</item>
回答5:
In my cellphone, IT'S work.
The solution is in below:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap
android:src="@drawable/frame_menu"
android:antialias="true"
android:gravity="center"
/>
</item>
<item >
<shape android:shape="rectangle">
<size android:width="24dp"/>
<solid android:color="@android:color/transparent"></solid>
<padding
android:right="10dp"
/>
</shape>
</item>
The most important sentence is "android:right=10dp"
回答6:
I would just add to @blacksh33p answer how to get the logo ImageView while using ActionBarSherlock to support older phones.
ImageView view = (ImageView)findViewById(android.R.id.home);
if (view == null)
view = (ImageView)findViewById(R.id.abs__home); // not sure that the name is exactly this - I tested it while developing an app on monodroid
回答7:
Simply Add the drawable Layout Call "actionbar_back_selector"
And put this Code
And in Java File (Which layout you need home up button that corresponding java file) Put this code
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.actionbar_back_selector);
回答8:
you may define a style for "HomeAsUpIndicator" in you theme like this:
<item name="toolbarNavigationButtonStyle">@style/MyToolbarNavigationButtonStyle</item>
in which, the toobarNavigationButtonStyle
is used to define all styles for that indicator, you may change it's width and paddingEnd to enlarge gap between it and the Title text.