[英]Toolbar with Custom View not Displaying with Full Width in android
我在這里找到了很多與工具欄相關的答案,但沒有一個答案可以幫助我。
我想要實現的是擁有一個擴展的工具欄,它將顯示一個徽標和菜單項。
我試圖實現這一點的方式是通過工具欄。 首先,我創建工具欄並添加我的自定義視圖。
我的toolbar.xml: -
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dl_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
android:clipToPadding="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#358c44"
android:layout_alignParentTop="true"/>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_alignParentBottom="true"
android:background="#358c44"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
我的名字活動: -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
toolbar.setLogo(R.mipmap.ic_launcher);
/* toolbar.setPadding(10,10,10,10);*/
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
我的應用程序顯示如下: -
我是android開發新手請幫幫我! 提前致謝
更新我的代碼我的應用程序看起來像: -
在工具欄上使用AppBar布局,請參閱此處的完整教程
試試這個它對我有用
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
// imageView for logo
<ImageView
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:scaleType="centerInside"
android:src="@drawable/title" />
// This is for search button on the right side
<ImageButton
android:id="@+id/imageButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="10dp"
android:src="@drawable/button_image"
android:scaleType="fitXY"
android:background="@android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="16dp" />
</FrameLayout>
</android.support.v7.widget.Toolbar>
在你的活動中,只需將其設置為這樣
Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
對於菜單在主活動中添加此項
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuSettings:
// Do some thing on setting press
break;
case R.id.menuAboutUs:
// Do some thing on About us
break;
}
return super.onOptionsItemSelected(item);
}
對於菜單項列表,在資源目錄下的菜單目錄中添加menu_main.xml文件,並在此文件中添加項目
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuSettings"
android:title="Settings" />
<item
android:id="@+id/menuAboutUs"
android:title="About Us" />
</menu>
檢查下面的代碼是否有效。 我在src \\ main \\ res \\ layout文件夾中使用了toolbar.xml文件。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:background="#2196F3"
android:layout_height="58dp"
android:id="@+id/relative">
<include
android:id="@+id/toolbarFav"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relative">
</LinearLayout>
</RelativeLayout>
toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="#FFFFFF"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.