繁体   English   中英

Android-无法为不同的活动添加不同的主题

[英]Android - Adding different themes for different activity not being applied

我是Android新手。 我想为所有活动创建一个具有不同主题的应用程序。 我为此进行了大量搜索,但是我找不到正确的解决方案。

这里的代码

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sample.webservice" >

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"

        >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name="com.example.sample.webservice.Usermain"
            android:label="@string/title_activity_usermain"
            android:theme="@style/UserMainTheme"
            android:parentActivityName="com.example.sample.webservice.MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.sample.webservice.MainActivity" />
        </activity>

    </application>

</manifest>

Styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:background">@color/white</item>
        <item name="android:windowBackground">@color/white</item>
        <item name="android:colorBackground">@color/white</item>

    </style>

    <style name="UserMainTheme" parent="android:Theme.Holo.Light">
        <item name="android:background">@color/white</item>
        <item name="android:windowBackground">@color/white</item>
        <item name="android:colorBackground">@color/white</item>
    </style>

</resources>

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:fontFamily="sans-serif"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/username"
            android:id="@+id/textView" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/username"
            android:layout_gravity="center_horizontal"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="@string/password"
            android:id="@+id/textView2" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/password"
            android:layout_gravity="right"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/login"
            android:id="@+id/login_button"
            android:layout_marginTop="@dimen/login_top_margin"
            android:layout_gravity="center_horizontal" />

    </LinearLayout>


</RelativeLayout>

activity_usermain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.sample.webservice.Usermain">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

package com.example.sample.webservice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button login = (Button) findViewById(R.id.login_button);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    setContentView(R.layout.activity_usermain);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这里是输出的屏幕截图:

MainActivity屏幕

在此处输入图片说明

用户主屏幕

在此处输入图片说明

以上两个屏幕具有相同的主题。 请在此先感谢我

我忘了用Intent开始活动。 这就是为什么不应用样式的原因。

必须改变这个

 public void onClick(View v) {
                    setContentView(R.layout.activity_usermain);
            }

MainActivity.java中的这个

public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, Usermain.class);
    startActivity(intent);
                }

它的工作很好。

兄弟,我也进行了一项活动,它并不是很复杂,只是为了测试这个问题,但我不需要启动任何代码。 如您在答案中所述。 对我来说很好。 我所做的就是稍微更改了Android Manifest文件和styles.xml。

只有这2个更改之后,我才出发

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM