ActionBar DropDown填充导致空格[横向模式]

时间:2015-06-11 15:30:20

标签: android android-layout android-styles

目前我有以下style.xml

更新 - 包括所有相关样式

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/MyActionBarStyle</item>
    <item name="actionDropDownStyle">@style/MyDropDownStyle</item>
</style>
<style name="MyActionBarStyle" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="android:actionBarDivider">@null</item>
    <item name="background">@drawable/logo</item>
    <item name="displayOptions">showHome</item>
</style>
<style name="MyDropDownStyle" parent="Base.Widget.AppCompat.Spinner.DropDown.ActionBar">
    <item name="android:paddingLeft">240dp</item>
    <item name="android:dropDownHorizontalOffset">240dp</item>
    <item name="android:background">@android:color/transparent</item>
</style>

这允许我将DropDown 240dp设置为远离其X轴,这非常有效。见截图#1

屏幕截图#1 enter image description here

但是作为副作用,这个填充会导致空白&#34;空间&#34;在弹出窗口的右侧。见截图#2

屏幕截图#2 enter image description here

如何设置固定大小的Popup或忽略DropDown样式设置的填充?

已更新

paddingLeft的原因是不与徽标(定义为背景)重叠

<item name="background">@drawable/logo</item>

更新2 - 没有dropDownHorizo​​ntalOffset样式的屏幕截图

enter image description here

PS:

dropDownHorizontalOffset仅用于设置Popup匹配的X轴以匹配paddingLeft

1 个答案:

答案 0 :(得分:0)

一种可行的解决方法是用新工具栏(android.support.v7.widget.Toolbar)替换ActionBar,并在其中添加一个Spinner(DropDown)

代码行已更改,以便更好地说明并且未经过测试

<强> main_activity.xml

<LinearLayout
    android:orientation="vertical"
    android:background="@color/navigationBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <android.support.v7.widget.Toolbar
            android:background="@drawable/logo"
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <Spinner
                android:layout_marginLeft="240dp"
                android:id="@+id/spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </android.support.v7.widget.Toolbar>
</LinearLayout>

<强> MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //toolbar.setLogo(R.drawable.logo);

    setupSpinnerTabs();

    // for device orientation purpose
    displaySpinner(getResources().getConfiguration().orientation);
}

private void setupSpinnerTabs() {
    ToolbarSpinnerAdapter adapter = new ToolbarSpinnerAdapter(toolbar.getContext());

    spinner = (Spinner)findViewById(R.id.spinner);
    spinner.setAdapter(adapter);
    // the below is used for the selection "change event" of the spinner
    //spinner.setOnItemSelectedListener(new SpinnerSelection());
}

<强> ToolbarSpinnerAdapter.java

public class ToolbarSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
    Context context;

    ToolbarSpinnerAdapter(Context ctx) {
        context = ctx;
    }

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object getItem(int position) {
        return "Spinner here"
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View defaultView = getLayoutInflater().inflate(R.layout.your_spinner_dropdown_layout, null);
        // [...]

        text.setText( this.getItem(position).toString() );
        return defaultView;
    }
}

结果看起来应该与下图相似:

enter image description here

PS:在纵向方向上,Spinner可能没有足够的空间