我在android studio中有一个计算器应用程序,可以很好地运行,但是我选择的主题为Material.Voice
,并在预览中显示。但是当我运行时,该应用程序又回到了常规主题。我该如何解决?这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这是我的样式文件:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
这是我的布局策略主文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp">
<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="number1"
android:inputType="number" />
<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="number2"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ToggleButton
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fed136"
android:text="Radians"/>
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radians"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView9"
app:layout_constraintTop_toBottomOf="@+id/switch3" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ToggleButton
android:id="@+id/switch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Degree"
android:background="#fed136" />
<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Degrees"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView9"
app:layout_constraintTop_toBottomOf="@+id/switch3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/sum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"></TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="+"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="-"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="x"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="/"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="SQ"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="SR"
android:background="#fed136"
android:layout_margin="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="C"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="E"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button10"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="%"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button18"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="sin"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button19"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="cos"
android:background="#fed136"
android:layout_margin="5dp"/>
<Button
android:id="@+id/button20"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="tan"
android:background="#fed136"
android:layout_margin="5dp"/>
</LinearLayout>
<Button
android:id="@+id/cube"
android:layout_width="48dp"
android:layout_height="50dp"
android:background="#fed136"
android:text="CU"
tools:text="CU" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="53dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="90dp"
android:layout_marginTop="0dp"
android:textSize="33sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="90dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="5"
android:stepSize="5"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
这是我的活动主要登陆文件(当您侧向转动手机时):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp">
<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="number1"
android:inputType="number" />
<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="number2"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ToggleButton
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fed136"
android:text="Radians"/>
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radians"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView9"
app:layout_constraintTop_toBottomOf="@+id/switch3" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ToggleButton
android:id="@+id/switch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Degree"
android:background="#fed136" />
<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Degrees"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView9"
app:layout_constraintTop_toBottomOf="@+id/switch3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="+" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="-" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="x" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="/" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="SQ" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="SR" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="C" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="E" />
<Button
android:id="@+id/button10"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="%" />
<Button
android:id="@+id/button18"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="sin" />
<Button
android:id="@+id/button19"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="cos" />
<Button
android:id="@+id/button20"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#fed136"
android:text="tan" />
</LinearLayout>
<Button
android:id="@+id/cube"
android:layout_width="98dp"
android:layout_height="50dp"
android:background="#fed136"
android:text="CU"
tools:text="CU" />
<TextView
android:id="@+id/sum"
android:layout_width="444dp"
android:layout_height="wrap_content"
android:textSize="20sp">
</TextView>
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="33sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="5"
android:stepSize="5"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
哦,以防万一您需要它,这是我的java文件:
package com.example.lenovouser.calculatoraghav;
//IMPORT
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
// ASSIGNING THE NAMES OR VARIABLES
TextView result, textView8;
EditText number1, number2;
Button add,subtract,divide,multiply,squareroot,square,cube,clear,Exponent,percent,sin
d, cosd, tand;
ToggleButton switch2, switch3;
double resultnum;
int num1,num2,num3;
boolean radient, degree;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ASSIGNING THE VARIABLES TO THE ANDROID WIDGETS.
result = (TextView)findViewById(R.id.sum);
number1 = (EditText)findViewById(R.id.number1);
number2 = (EditText)findViewById(R.id.number2);
add = (Button)findViewById(R.id.button);
subtract = (Button)findViewById(R.id.button2);
multiply = (Button)findViewById(R.id.button3);
divide = (Button)findViewById(R.id.button4);
squareroot =(Button)findViewById(R.id.button6);
square = (Button)findViewById(R.id.button5);
clear = (Button)findViewById(R.id.button8);
Exponent = (Button)findViewById(R.id.button9);
percent = (Button)findViewById(R.id.button10);
sind = (Button)findViewById(R.id.button18);
cosd = (Button)findViewById(R.id.button19);
tand = (Button)findViewById(R.id.button20) ;
switch2 = (ToggleButton) findViewById(R.id.switch2);
switch3 = (ToggleButton) findViewById(R.id.switch3);
textView8 = (TextView)findViewById(R.id.textView8);
cube = (Button)findViewById(R.id.cube);
add.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//ADD
num1 = Integer.parseInt(number1.getText() .toString());
num2 = Integer.parseInt(number2.getText() .toString());
resultnum = num1 + num2;
result.setText(String.valueOf(resultnum));
}
});
subtract.setOnClickListener(new View.OnClickListener(){
//SUBTRACT
@Override
public void onClick(View v){
num1 = Integer.parseInt(number1.getText() .toString());
num2 = Integer.parseInt(number2.getText() .toString());
resultnum = num1 - num2;
result.setText(String.valueOf(resultnum));
}
});
multiply.setOnClickListener(new View.OnClickListener(){
//MULTIPLY
@Override
public void onClick(View v){
num1 = Integer.parseInt(number1.getText() .toString());
num2 = Integer.parseInt(number2.getText() .toString());
resultnum = num1 * num2;
result.setText(String.valueOf(resultnum));
}
});
divide.setOnClickListener(new View.OnClickListener(){
//DIVIDE
@Override
public void onClick(View v){
num1 = Integer.parseInt(number1.getText() .toString());
num2 = Integer.parseInt(number2.getText() .toString());
resultnum = num1 / num2;
result.setText(String.valueOf(resultnum));
}
});
squareroot.setOnClickListener(new View.OnClickListener(){
//SQUAREROOT
@Override
public void onClick(View v){
num1 = Integer.parseInt(number1.getText() .toString());
resultnum = (Math.sqrt(num1));
result.setText(String.valueOf(resultnum));
}
});
square.setOnClickListener(new View.OnClickListener(){
//SQUARE
@Override
public void onClick(View v){
num1 = Integer.parseInt(number1.getText() .toString());
resultnum = num1*num1;
result.setText(String.valueOf(resultnum));
}
});
cube.setOnClickListener(new View.OnClickListener(){
//SQUARE
@Override
public void onClick(View v){
num1 = Integer.parseInt(number1.getText() .toString());
resultnum = num1*num1*num1;
result.setText(String.valueOf(resultnum));
}
});
clear.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//CLEAR
num1 = (0);
num2 = (0);
num3 = (0);
number1.setText("");
number2.setText("");
//txt.setText("");
resultnum = 0;
result.setText(String.valueOf(resultnum));
}
});
Exponent.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//EXPONENT
int p = Integer.parseInt(number1.getText() .toString());
int e = Integer.parseInt(number2.getText() .toString());
resultnum = (Math.pow((double)p, (double)e));
result.setText(String.valueOf(resultnum));
}
});
percent.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//PERCENT
Double percentage =
Double.valueOf(number1.getText().toString());
Integer num2 = Integer.parseInt(number2.getText().toString());
Double resultnum = percentage * num2 / 100;
result.setText(String.valueOf(resultnum));
}
});
sind.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//SIN num2
if (degree)
{
num1 = Integer.parseInt(number1.getText().toString());
//num2 = Integer.parseInt(number2.getText().toString());
resultnum = Math.sin(Math.toDegrees(num1));
result.setText(String.valueOf(resultnum));
}
if (radient){
num1 = Integer.parseInt(number1.getText().toString());
//num2 = Integer.parseInt(number2.getText().toString());
resultnum = Math.sin(Math.toRadians(num1));
result.setText(String.valueOf(resultnum));
if (!degree)
{
}
if (!radient){
}
}
}
});
cosd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//COS num2
if (degree)
{
num1 = Integer.parseInt(number1.getText().toString());
// num2 = Integer.parseInt(number2.getText().toString());
resultnum = Math.cos(Math.toDegrees(num1));
result.setText(String.valueOf(resultnum));
}
if (radient){
num1 = Integer.parseInt(number1.getText().toString());
//num2 = Integer.parseInt(number2.getText().toString());
resultnum = Math.cos(Math.toRadians(num1));
result.setText(String.valueOf(resultnum));
}
if (!degree)
{
}
if (!radient){
}
}
});
tand.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//TAN num2
if (degree)
{
num1 = Integer.parseInt(number1.getText().toString());
//num2 = Integer.parseInt(number2.getText().toString());
resultnum = Math.tan(Math.toDegrees(num1));
result.setText(String.valueOf(resultnum));
}
if (radient){
num1 = Integer.parseInt(number1.getText().toString());
// num2 = Integer.parseInt(number2.getText().toString());
resultnum = Math.tan(Math.toRadians(num1));
result.setText(String.valueOf(resultnum));
}
if (!degree)
{
}
if (!radient){
}
}
});
switch2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//switchR
radient = true;
degree = false;
textView8.setText(("Radians MODE"));
}
});
switch3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//switchD
degree = true;
radient = false;
textView8.setText(("Degree MODE"));
}
});
}}