[英]How to align Button in a custom ToolBar to Right
我制作了一個自定義工具欄,但最近決定在其上添加一個按鈕作為主頁按鈕。 我還在 java 中定義了它,它在單擊按鈕時工作,但工具欄標題文本顯示一半,我試圖 alignParentRight 但它不起作用。 請幫我
下面是我的代碼和我嘗試過的。
TIps_4.java 活動
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);
}
}
工具欄_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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.