我想在mainActivity中设置一个自定义ActionBar。我为名为<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>5.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
的操作栏进行了自定义布局。并且宽度在custom_layout中是match_parent,并且在Activity中设置Layoutparams也是MATCH_PARENT。但它仍然在左右两侧留有边距。
主要活动
custom_header
custom_header.xml
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT
);
View viewActionBar = ((Activity) MainActivity.this).getLayoutInflater().inflate(R.layout.custom_header,null);
actionBar.setCustomView(viewActionBar,params);
mainactivity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:padding="10dp"
android:background="@color/md_white_1000"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/logo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/txtHead"
android:text="DIGIM"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="KEY"
android:textSize="20sp"
android:textColor="@color/appdefault"
android:layout_toRightOf="@+id/txtHead"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:src="@drawable/notification_icon"
android:background="@android:color/transparent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_width="30dp"
android:layout_height="30dp" />
</RelativeLayout>
style.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
只需添加
Toolbar toolbar=(Toolbar)viewActionBar.getParent();
toolbar.setContentInsetsAbsolute(0,0);
在下面提到的行之后
actionBar.setCustomView(viewActionBar,params);
答案 1 :(得分:0)
从RelativeLayout中删除android:padding="10dp"