为什么Material Design Toolbar有这种奇怪的填充?

时间:2016-05-22 23:48:28

标签: android material-design android-toolbar layoutparams android-layoutparams

ToolBar android:padding=0dp LinearLayout中,我有一个android:width=match_parent android:layout_marginLeft="0dp"Toolbar。因此,至少在宽度方面,LinearLayout(本例中为黑色)的任何部分都不应显示在package tests.example_one; import android.annotation.SuppressLint; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; @SuppressLint("NewApi") public class MainActivity extends AppCompatActivity { private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.mainActivity_appBar); setSupportActionBar(toolbar); } } 的两侧(此SSCCE中的红色)。

问题是工具栏显示在子LinearLayout的左侧。如何让LinearLayout跨越整个工具栏,以便工具栏的任何部分都不显示?

enter image description here

MainActivity.java:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/fullBackgroundPadding" >


    <include layout="@layout/app_bar"
        android:id="@+id/mainActivity_appBar" />

</LinearLayout>

RES /布局/ activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/appBarHeight"
    android:background="#000"
    android:padding="0dp" >


    <LinearLayout android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F44336"
        android:layout_marginLeft="0dp">

    </LinearLayout>


</android.support.v7.widget.Toolbar>

RES /布局/ app_bar.xml

<resources>

    <dimen name="fullBackgroundPadding">8dp</dimen>

    <!-- App Bar -->
    <dimen name="appBarHeight">48dp</dimen>

</resources>

RES /值/ dimens.xml

public class TabFan extends Fragment {

//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //Returning the layout file after inflating
    //Change R.layout.tab1 in you classes
    return inflater.inflate(R.layout.tab_fan, container, false);

    // Onclick Listening
    ImageView image = (ImageView) findViewById(R.id.image);
    image.setOnClickListener(this);


}
public void onClick(View v) {

    // Launching new Activity on hitting the image
    Intent j = new Intent(getApplicationContext(), Activity2.class);
    startActivity(j);
    // End intent
}

1 个答案:

答案 0 :(得分:2)

黑色部分是工具栏应用于其内容的插图。将以下两行添加到Toolbar的XML:

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

另外,不要忘记app命名空间。

所以你实际上是在改变你的app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/appBarHeight"
    android:background="#000"
    android:padding="0dp" >


    <LinearLayout android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F44336"
        android:layout_marginLeft="0dp">

    </LinearLayout>


</android.support.v7.widget.Toolbar>

<?xml version="1.0" encoding="utf-8"?>
<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="@dimen/appBarHeight"
    android:background="#000"
    android:padding="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetLeft="0dp" >


    <LinearLayout android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F44336">

    </LinearLayout>


</android.support.v7.widget.Toolbar>