我的工具栏没有一直延伸到整个屏幕(android)

时间:2018-03-08 20:27:51

标签: android android-toolbar

我正在做一个非常简单的活动,但我无法让工具栏一直延伸到整个屏幕。这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:id="@+id/linearLayout"
  android:background="@drawable/backgroundgradient"
  tools:context="com.webnation.text2email.DisplayTextActivity">

<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:local="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:minHeight="?attr/actionBarSize"
  android:background="@color/colorPrimary"
  local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<ScrollView
    android:id="@+id/widget54"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    >
        <TextView
            android:id="@+id/displayText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dip"
            android:text="@string/eula"
            tools:context=".DisplayText" />

</ScrollView>

</LinearLayout>

我的活动:

open class DisplayTextActivity : AppCompatActivity() {

private var nameOfFile = ""
lateinit var androidText: AndroidText

public override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)

    setContentView(R.layout.displaytext)
    linearLayout.background = ContextCompat.getDrawable(this, Globals.getBackground(this))
    val toolbar = findViewById(R.id.toolbar) as Toolbar
    setSupportActionBar(toolbar)
    val actionBar = supportActionBar
    val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
    val theme = preferences.getString(Globals.KEY_THEME, Globals.THEME_PRIMARY)
    val iTheme = Globals.getTheme(theme)

    val extras = intent.extras
    if (extras != null) {
        nameOfFile = extras.getString(Globals.keyFileName, Globals.FILE_EULA)
        try {
            androidText = AndroidText(nameOfFile, this@DisplayTextActivity) //Get the text of the eula from text file
            displayText.text = Html.fromHtml(androidText.androidText)
        } catch (e: Resources.NotFoundException) {
            Timber.e(e, e.toString())
            val dialog = WebNationAlertDialog(this@DisplayTextActivity)
            dialog.setTitle("Error!")
            dialog.setDialogText(resources.getString(R.string.resource_not_found))

            dialog.setNeuText(resources.getString(R.string.ok)) {
                dialog.dismiss()
                setResult(Activity.RESULT_OK)
                finish()
            }

            dialog.show()
        }
        Globals.setToolBarColor(iTheme, toolbar)
        try {
            assert(actionBar != null)
            actionBar?.setDisplayHomeAsUpEnabled(true)
            actionBar?.setHomeButtonEnabled(true)
            actionBar?.setDisplayShowTitleEnabled(true)
            var title = resources.getString(R.string.eulaTitle)
            when (nameOfFile) {
                Globals.FILE_EULA -> {
                    title = resources.getString(R.string.eulaTitle)
                }
                Globals.FILE_HELP -> {
                    title = resources.getString(R.string.fileHelpTitle)
                }
            }
            actionBar?.setTitle(title)
        } catch (ignored: Exception) {
            Timber.e(ignored.toString())
        }

    } else {
        setResult(Activity.RESULT_OK)
        finish()
    }


}
override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
    if (menuItem.itemId == android.R.id.home) {
        finish()
    }
    return super.onOptionsItemSelected(menuItem)
}
}

以下是截图的样子。我不确定是什么问题,因为我在其他几个活动中使用了这种布局的版本。

Screenshot

1 个答案:

答案 0 :(得分:1)

我怀疑您的父级LinearLayout中的android:background="@drawable/backgroundgradient"内有填充。从backgroundgradient中删除该填充或将填充应用于除工具栏之外的其他视图。你有多种选择。祝你好运!