Android ActionBar setActionView layout issue

让人想犯罪 __ 提交于 2019-12-01 17:48:18

问题


I've been trying to use the setActionView from the ActionBar in ICS

Seems like it should be straight forward but somehow I'm not getting the layout alignment that I would hope for. As you can see in the image below the 'target' icon is centered correctly within it's layout. But when I setActionBar(progress) the progress view is always aligned to the right whatever I try.

Here are the 2 states, before and after clicking the menu item. As you can see the progress view is always aligned to the right. I've tried changing the gravity options in the my progress layout xml from left to right to center and whatever I do change it doesn't seem to change anything.

I haven't found any info regarding this problem so I'm thinking I must be doing something wrong.

Do anyone have a clue? Thanks for the help!

Here's my action bar menu layout 'action_bar_menu.xml'

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/locate"
          android:title="locate"
          android:icon="@drawable/locate"
          android:showAsAction="ifRoom|withText" />
</menu>

Here's my progressbar layout 'inderterminate_progress.xml'

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:gravity="center">

    <ProgressBar android:layout_width="25dp"
                 android:layout_height="25dp"
                 android:layout_gravity="center"
                 android:indeterminate="true"
                 style="?android:attr/progressBarStyleInverse"/>
</FrameLayout>

And finally here's my testx Activity

public class HelloAndroidActivity extends Activity {

    /**
     * Called when the activity is first created.
     * @param savedInstanceState If the activity is being re-initialized after
     * previously being shut down then this Bundle contains the data it most
     * recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getActionBar().setTitle("Test");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_bar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);


        if (R.id.locate == item.getItemId()) {

            final MenuItem menuItem = item.setActionView(R.layout.inderterminate_progress);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            menuItem.setActionView(null);
                        }
                    });
                }
            }).start();
        }

        return true;
    }
}

回答1:


It seems that explicitly defining the style and adding a 4dip padding in the root of the layout to inflate as shown below solves this issue

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingLeft="4dip"
    android:paddingRight="4dip"
    style="?android:attr/actionButtonStyle" />

This seems to be used in the android stylesheet here




回答2:


A little late but the right solution is not an absolute value such as 4dp. The right approach is to set the minWidth to ABS values:

android:minWidth="@dimen/abs__action_button_min_width"

Example progress bar:

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

<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
             android:minWidth="@dimen/abs__action_button_min_width"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:focusable="true"
             android:layout_gravity="center"
             style="@android:style/Widget.ProgressBar.Small"/>


来源:https://stackoverflow.com/questions/10646150/android-actionbar-setactionview-layout-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!