尽管已明确设置ActionBar颜色,但未设置

时间:2019-07-19 18:38:55

标签: android kotlin

因此,我希望我的操作栏为粉红色,并在操作栏的右侧具有某些按钮!所以我添加了自定义ToolBar小部件,但是没有设置我的颜色,是怎么回事:

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="Theme.Base" parent="Theme.AppCompat.Light"></style>

</resources>

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.halalrishtey">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.kt

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val myWebView = findViewById<WebView>(R.id.webview)
    setSupportActionBar(findViewById(R.id.my_toolbar))

    actionBar?.setBackgroundDrawable(ColorDrawable(Color.parseColor("#ed8d8e")))
    actionBar?.title = "My App"
    //More code here...

}

}

1 个答案:

答案 0 :(得分:0)

您可以像这样在xml中添加它:

                    <android.support.v7.widget.Toolbar
                    android:id="@+id/my_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#ed8d8e"
                    />