如何将自定义工具栏中的按钮右对齐

How to align Button in a custom ToolBar to Right

我制作了一个自定义工具栏,但最近决定在其上添加一个按钮作为主页按钮。我还在 java 中定义了它,当单击按钮但工具栏标题文本显示 half-way 时它可以工作,我尝试过 alignParentRight 但它不起作用。 请帮助我

下面是我的代码和我尝试过的代码。

TIps_4.java Activity

public class Tips_4 extends AppCompatActivity {
    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    Button Tips3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the start page passed to us or default to first page
        Intent mIntent = getIntent();
        int startPage = mIntent.getIntExtra("startPage", 0);
        setContentView(R.layout.activity_tips_4);

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


        Button toolbar_btn = (Button)findViewById(R.id.toolbarButton);
        toolbar_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent toolbarIntent = new Intent(Tips_4.this, MainActivity.class);
                toolbarIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                Tips_4.this.startActivity(toolbarIntent);
                setSupportActionBar(toolbar);
            }
        });



        ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(new SimpleFragmentPageAdapter(getSupportFragmentManager(),this));
        // set the current page to the start page
        viewPager.setCurrentItem(startPage);

        //Attach the page change listener inside the activity
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            // This method will be invoked when a new page becomes selected
            @Override
            public void onPageSelected(int position) {
                Toast.makeText(Tips_4.this, "Selected Maths Page:" + position, Toast.LENGTH_SHORT).show();
            }
            // This method will be invoked when the current page is scrolled
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetpixels) {

            }
            // Called when the scroll state changes:
            //SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        tabLayout= (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);


    }



}

toolbar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:subtitleTextColor="#FF9800"
    android:background="@color/colorPrimary">

    <Button
        android:id="@+id/toolbarButton"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:foreground="@drawable/ic_home_24dp"
        android:layout_marginLeft="300dp"
        android:background="#000"/>

</androidx.appcompat.widget.Toolbar>

这就是我拒绝工作的做法

 <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:orientation="horizontal"
        android:layout_alignParentRight="true">


        <Button
            android:id="@+id/toolbarButton"
            android:layout_width="54dp"
            android:layout_height="50dp"
            android:background="#056359"
            android:layout_gravity="end"
            android:padding="11dp"
            android:foreground="@drawable/ic_home_24dp" />

    </RelativeLayout>

这是截图

这是最后一个带有按钮的工具栏的完整代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:subtitleTextColor="#FF9800"
    android:background="@color/colorPrimary">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mathematics Pages"
            android:textSize="23sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:gravity="right"/>

        <Button
            android:id="@+id/toolbarButton"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:foreground="@drawable/ic_home_24dp"
            android:layout_alignParentEnd="true"
            android:background="#000"
            android:layout_alignParentRight="true" />

    </RelativeLayout>
</androidx.appcompat.widget.Toolbar>

您可以使用相对布局并将其宽度和高度设置为 match_parent 现在在您的相对布局中添加按钮并将 alignParentEnd 设置为 true

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:subtitleTextColor="#FF9800"
    android:background="@color/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/toolbarButton"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:foreground="@drawable/ic_home"
            android:layout_alignParentEnd="true"
            android:background="#000"/>

    </RelativeLayout>
</androidx.appcompat.widget.Toolbar>

如果您只想在按钮中使用图标,您可以使用与普通按钮一样工作的 ImageButton。如果你的图标是固定大小的,比如 24 X 24 那么你可以将按钮的宽度和高度都设置为 wrap_content。