Android - 为未应用的 activity 添加不同的主题
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 开始 activity。这就是未应用样式的原因。
必须改变这个
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);
}
效果很好。
兄弟,我也做了一个 activity 也不是很复杂,只是为了测试这个问题,但我不需要启动任何代码。正如您在答案中所描述的那样。它对我来说很好用。我所做的只是稍微更改了我的 Android 清单文件和 styles.xml。
仅进行了这 2 次更改后,我就可以开始了
我是 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 开始 activity。这就是未应用样式的原因。
必须改变这个
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);
}
效果很好。
兄弟,我也做了一个 activity 也不是很复杂,只是为了测试这个问题,但我不需要启动任何代码。正如您在答案中所描述的那样。它对我来说很好用。我所做的只是稍微更改了我的 Android 清单文件和 styles.xml。
仅进行了这 2 次更改后,我就可以开始了