将自定义ActionBar重力设置为RIGHT

时间:2018-08-10 13:02:59

标签: android

abs_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentlay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/mytext"
        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:text="test"
        android:textColor="#474545"
        android:textSize="18sp" />

</LinearLayout>

当我使用此代码设置自定义ActionBar时,一切都很好,并显示在右侧:

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.abs_layout);

但是当我尝试使用充气机在自定义操作栏上更改textview的图块时,文本会停留在左侧:

   LayoutInflater mInflater = getLayoutInflater();

    View mCustomView = mInflater.inflate(R.layout.abs_layout, null);
    TextView tv = mCustomView.findViewById(R.id.mytext);
    tv.setText("test");
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(mCustomView);

我希望它在右侧。

1 个答案:

答案 0 :(得分:1)

尝试一下:

styles.xml

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

 <style name="CustomTheme" parent="Theme.AppCompat.Light">

    <item name="contentInsetStart">0dp</item>
    <item name="contentInsetEnd">0dp</item>
  </style>

</resources>

custom_action_bar_layout.xml

<TableRow>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/back"
        android:layout_gravity="center_vertical"
        android:background="@android:color/transparent"
        android:id="@+id/action_bar_back"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:padding="10dp"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/forward"
        android:id="@+id/action_bar_forward"
        android:layout_gravity="center_vertical"
        android:background="@android:color/transparent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />


      </TableRow>

   </TableLayout>

Android自定义操作条形码

package com.journaldev.customactionbar;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setCustomView(R.layout.custom_action_bar_layout);
    View view =getSupportActionBar().getCustomView();

    ImageButton imageButton= (ImageButton)view.findViewById(R.id.action_bar_back);

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    ImageButton imageButton2= (ImageButton)view.findViewById(R.id.action_bar_forward);

    imageButton2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Forward Button is clicked",Toast.LENGTH_LONG).show();
        }
    });
}

}

注意:在AndroidManifest.xml的application标签内添加以下行。

  android:theme="@style/CustomTheme"

它可以帮助您