我正在尝试根据选中的单选按钮(参见下面的代码)以编程方式更改我的应用程序的整体样式(背景颜色、字体和字体大小)。我已经读到,为了让它工作,我必须在 onCreate 方法中编写它,因为 setTypeFace 或 setTextSize 无法在其他函数中解析。
我也尝试在另一个函数中调用这两个方法,这个函数叫做setStyles,但是这两个方法仍然无法解析,我也不知道为什么。 我确实为字体导入了 android.graphics.Typeface。
下面是我的 Java 代码和我的两个 XML 代码。
完整的 Java 代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout buttonMenu = findViewById(R.id.dynamic_btn);
Button showBtn = new Button(this);
showBtn.setText(R.string.menu);
showBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
showBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
//setStyles(v);
final View myView = getLayoutInflater().inflate(R.layout.style, null);
final AlertDialog.Builder diag = new AlertDialog.Builder(getApplicationContext());
diag.setTitle("Configuration de l'application");
diag.setPositiveButton("Enregistrer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
RadioGroup radioColGroup = (RadioGroup) ((Dialog)dialog).findViewById(R.id.radioColor);
int col = Color.parseColor("#000000"); // background color
int selectedId = radioColGroup.getCheckedRadioButtonId();
Typeface type = getResources().getFont(R.font.fff_tusj);
if (selectedId == R.id.style1) {
// change background, font and size
col = Color.parseColor("#000000");
type = getResources().getFont(R.font.fff_tusj);
v.setTextSize(40);
}
if (selectedId == R.id.style2) {
// change background, font and size
col = Color.parseColor("#00574A");
type = getResources().getFont(R.font.sansation_light);
v.setTextSize(50);
}
else if (selectedId == R.id.style3) {
// change background, font and size
col = Color.parseColor("#6B0504");
type = getResources().getFont(R.font.oswald_stencil);
v.setTextSize(30);
}
View background = findViewById(R.id.layoutBackground);
background.setBackgroundColor(col);
v.setTypeFace(type);
}
});
diag.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
diag.setView(R.layout.style);
diag.show();
}
});
if(buttonMenu != null) {
buttonMenu.addView(showBtn);
}
}
public void setStyles(View view) {
final View myView = getLayoutInflater().inflate(R.layout.style, null);
final AlertDialog.Builder diag = new AlertDialog.Builder(this);
diag.setTitle("Configuration de l'application");
diag.setPositiveButton("Enregistrer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
RadioGroup radioColGroup = (RadioGroup) ((Dialog)dialog).findViewById(R.id.radioColor);
int col = Color.parseColor("#000000"); // background color
int selectedId = radioColGroup.getCheckedRadioButtonId();
Typeface type = Typeface.createFromAsset(getAssets(), "fff_tusj.ttf");
if (selectedId == R.id.style1) {
// change background, font and size
col = Color.parseColor("#000000");
type = Typeface.createFromAsset(getAssets(), "oswald_stencil.ttf");
//myView.setTextSize();
}
if (selectedId == R.id.style2) {
// change background, font and size
col = Color.parseColor("#00574A");
type = Typeface.createFromAsset(getAssets(), "sansation_light.ttf");
}
else if (selectedId == R.id.style3) {
// change background, font and size
col = Color.parseColor("#6B0504");
}
View background = findViewById(R.id.layoutBackground);
background.setBackgroundColor(col);
//myView.setTypeFace(type);
}
});
diag.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
diag.setView(R.layout.style);
diag.show();
}
}
activity_main.xml:
<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"
tools:context=".MainActivity"
android:orientation="vertical"
android:id="@+id/layoutBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dynamic_btn"
android:layout_weight="1"/>
<EditText
android:id="@+id/saisie_recherche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:inputType="text"/>
<Button
android:id="@+id/recherche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recherche"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/resultats"/>
<!-- dynamic menu -->
</LinearLayout>
样式.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/style">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioColor"
android:orientation="horizontal"
android:layout_margin="50px"
android:gravity="center">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/style1"
android:text="style 1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/style2"
android:text="style 2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/style3"
android:text="style 3"/>
</RadioGroup>
</LinearLayout>
最佳答案
如果有人对答案感兴趣,我实际上改变了我做事的方式。
为了更改字体、字体大小和背景颜色,我在布局文件 style.xml 中创建了新主题并声明了三个新主题。
然后根据点击的单选按钮(如果有条件),我使用了setTheme(nameOfTheme)。
有效。
关于java - 用 Java 改变我的 Android 应用的整体风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59343478/