簡體   English   中英

在操作欄中填充水平的“自定義視圖”

[英]Fill horizontal Custom View in action bar

我想顯示在操作欄上使用自定義視圖,該視圖完全覆蓋了操作欄(寬度匹配父項)

但是我的自定義視圖不能覆蓋操作欄的整個寬度。

在此處輸入圖片說明

public class MyActivity extends AppCompatActivity {

    private ActionBar actionBar;

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

    private void setActionBar() {
        actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        View abView = actionBar.getCustomView();
        LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutParams layout = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        View v = inflator.inflate(R.layout.action_bar, null);
        actionBar.setCustomView(v, layout);
        actionBar.setDisplayShowCustomEnabled(true);
    }

action_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/mvp_logo"
        android:layout_alignParentRight="true" />

</RelativeLayout>

我引用了以下鏈接:

如何在ActionBar中顯示自定義視圖?

手動填充自定義視圖會為ActionBar自定義視圖產生不同的布局

動作欄中的Android Center自定義視圖

Android:如何在操作欄中的自定義視圖中將組件居中放置?

謝謝

工具欄可能更適合您,但請注意,這違反了設計准則,將使您的應用不適合Android生態系統,該區域通常是上下文菜單所在的位置,並且大多數用戶會了解到行為。

您可以通過刪除內容插入來解決此問題,如下所示

View myLogo = mInflater.inflate(R.layout.actionbar_custom, null);
getSupportActionBar().setCustomView(myLogo);
((Toolbar) myLogo.getParent()).setContentInsetsAbsolute(0,0);

如果您已經更改為工具欄,則可以在XML中針對自定義視圖對其進行修復

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM