英文:
How to change drawable source from java file?
问题
Java文件:
public void LanguageButtonClick(View view)
{
Button lngBtn = findViewById(R.id.languageButton);
if(currentLanguage == Languages.English)
{
currentLanguage = Languages.Polish;
lngBtn.setText("pl");
// 设置 android:drawableLeft="@drawable/ic_poland"
}
else if (currentLanguage == Languages.Polish)
{
currentLanguage = Languages.English;
lngBtn.setText("eng");
// 设置 android:drawableLeft="@drawable/ic_united_kingdom"
}
}
XML文件:
<Button
android:onClick="LanguageButtonClick"
android:id="@+id/languageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:width="125dp"
android:height="45dp"
android:background="@drawable/language_button"
android:drawableLeft="@drawable/ic_united_kingdom" <!-- 这部分 -->
android:text="@string/eng"
android:textSize="18sp"
android:textStyle="bold"
app:backgroundTint="#FFFFFF"
app:backgroundTintMode="multiply"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
英文:
So, I have a button to switch current language. It shows flag and country name next to it [(Flag) ENG] I want to change text and image when user clicks it, so it changes application language and displays current. I know how to change text, but have no clue how to make image change it source
Java file
public void LanguageButtonClick(View view)
{
Button lngBtn = findViewById(R.id.languageButton);
if(currentLanguage == Languages.English)
{
currentLanguage = Languages.Polish;
lngBtn.setText("pl");
//Set android:drawableLeft="@drawable/ic_poland"
}
else if (currentLanguage == Languages.Polish)
{
currentLanguage = Languages.English;
lngBtn.setText("eng");
///Set android:drawableLeft="@drawable/ic_united_kingdom"
}
}
xml file
<Button
android:onClick="LanguageButtonClick"
android:id="@+id/languageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:width="125dp"
android:height="45dp"
android:background="@drawable/language_button"
android:drawableLeft="@drawable/ic_united_kingdom" <------------------This one
android:text="@string/eng"
android:textSize="18sp"
android:textStyle="bold"
app:backgroundTint="#FFFFFF"
app:backgroundTintMode="multiply"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
答案1
得分: 0
您可以使用 `setCompoundDrawables` 方法
```java
Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_united_kingdom);
lngBtn.setCompoundDrawables(drawable, null, null, null);
以下是完整代码
public void LanguageButtonClick(View view)
{
Button lngBtn = findViewById(R.id.languageButton);
if(currentLanguage == Languages.English)
{
currentLanguage = Languages.Polish;
lngBtn.setText("pl");
Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_poland);
lngBtn.setCompoundDrawables(drawable, null, null, null);
}
else if (currentLanguage == Languages.Polish)
{
currentLanguage = Languages.English;
lngBtn.setText("eng");
Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_united_kingdom);
lngBtn.setCompoundDrawables(drawable, null, null, null);
}
}
英文:
You can use the setCompoundDrawables
Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_united_kingdom);
lngBtn.setCompoundDrawables(drawable, null, null, null);
Here's the full code
public void LanguageButtonClick(View view)
{
Button lngBtn = findViewById(R.id.languageButton);
if(currentLanguage == Languages.English)
{
currentLanguage = Languages.Polish;
lngBtn.setText("pl");
Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_poland);
lngBtn.setCompoundDrawables(drawable, null, null, null);
}
else if (currentLanguage == Languages.Polish)
{
currentLanguage = Languages.English;
lngBtn.setText("eng");
Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_united_kingdom);
lngBtn.setCompoundDrawables(drawable, null, null, null);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论