无法在应用栏中左对齐自定义布局

时间:2016-12-09 15:36:03

标签: android android-appcompat android-actionbar-compat appbar

我已经处理了很长一段时间了。虽然没有成功! 我想要的是将自定义布局设置在应用栏的最左侧。没有任何边缘。但无论我改变什么,布局都不会再进一步​​了。

enter image description here

我的styles.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- colorPrimary is used for the default action bar background -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" >
    <item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="lollipop">true</item>
    <!--<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>-->
    <item name="colorPrimary">@color/blue4main</item>
    <item name="colorPrimaryDark">@color/blue5</item>
    <item name="android:textColor">@color/black1</item>
    <item name="android:textColorPrimary">@color/blue6</item>
    <item name="android:textColorSecondary">@color/blue1</item>
    <item name="android:textColorPrimaryInverse">@color/blue3</item>
    <!---->
    <item name="android:contentInsetLeft" tools:targetApi="lollipop">0dp</item>
    <item name="android:contentInsetStart" tools:targetApi="lollipop">0dp</item>

我的custom_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_gravity="left"
          android:background="@color/black1"
          android:gravity="left|center_vertical"
          android:layoutDirection="ltr"
          android:contentInsetStart="0dp"
          android:contentInsetLeft="0dp"
          app:contentInsetLeft="0dp"
          app:contentInsetStart="0dp"
          android:orientation="horizontal">
   ...

和代码:

    private void insideOnCreateForVicinity() {
    ActionBar actionBar = getSupportActionBar(); // you can use ABS or the non-bc ActionBar
    if (actionBar != null) {
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM  |  ActionBar.DISPLAY_HOME_AS_UP |ActionBar.DISPLAY_SHOW_TITLE);
    }

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.vicinity_and_filtercone_search_bar, null);
    // the view that contains the search "magnifier" icon
    final ImageView searchIcon = (ImageView) layout.findViewById(R.id.search_mag_icon);
    final ImageView filterConeIcon = (ImageView) layout.findViewById(R.id.ic_filter_cone);
    final DelayAutoCompleteTextView textViewVicinity = (DelayAutoCompleteTextView) layout.findViewById(R.id.text_view_vicinity);
    textViewVicinity.setThreshold(3);//after how many letters users types, send the request.
    textViewVicinity.setAdapter(new VicinityAutoCompleteAdapter(this));
    textViewVicinity.setLoadingIndicator((android.widget.ProgressBar) layout.findViewById(R.id.progress_bar));
    textViewVicinity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            VicinityPageItem p = (VicinityPageItem) adapterView.getItemAtPosition(position);
            textViewVicinity.setText(p.Name);
            textViewVicinity.setSelection(textViewVicinity.length());//put the cursor at the end
            searchFilter.setVicinityID(p.ID);//hamed. kesafate in
            reload(searchFilter);
        }
    });
    actionBar.setCustomView(layout);

我已尝试过下面的链接以及其他一些不为我工作的链接:

How to align items in action bar to the left? Add custom layout to ActionBar with Gravity Left? Android API 21 Toolbar Padding

1 个答案:

答案 0 :(得分:0)

感谢Amrut Bidri this answer,我找到了解决方案: (但我仍然不知道为什么在布局文件中设置引力不起作用)

    ActionBar.LayoutParams lp = (ActionBar.LayoutParams) layout.getLayoutParams();
    lp.gravity = Gravity.LEFT;
    layout.setLayoutParams(lp);