如何从Java文件中更改可绘制资源的来源?

huangapple go评论136阅读模式
英文:

How to change drawable source from java file?

问题

Java文件:

  1. public void LanguageButtonClick(View view)
  2. {
  3. Button lngBtn = findViewById(R.id.languageButton);
  4. if(currentLanguage == Languages.English)
  5. {
  6. currentLanguage = Languages.Polish;
  7. lngBtn.setText("pl");
  8. // 设置 android:drawableLeft="@drawable/ic_poland"
  9. }
  10. else if (currentLanguage == Languages.Polish)
  11. {
  12. currentLanguage = Languages.English;
  13. lngBtn.setText("eng");
  14. // 设置 android:drawableLeft="@drawable/ic_united_kingdom"
  15. }
  16. }

XML文件:

  1. <Button
  2. android:onClick="LanguageButtonClick"
  3. android:id="@+id/languageButton"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:layout_marginTop="16dp"
  7. android:layout_marginRight="16dp"
  8. android:width="125dp"
  9. android:height="45dp"
  10. android:background="@drawable/language_button"
  11. android:drawableLeft="@drawable/ic_united_kingdom" <!-- 这部分 -->
  12. android:text="@string/eng"
  13. android:textSize="18sp"
  14. android:textStyle="bold"
  15. app:backgroundTint="#FFFFFF"
  16. app:backgroundTintMode="multiply"
  17. app:layout_constraintEnd_toEndOf="parent"
  18. 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

  1. public void LanguageButtonClick(View view)
  2. {
  3. Button lngBtn = findViewById(R.id.languageButton);
  4. if(currentLanguage == Languages.English)
  5. {
  6. currentLanguage = Languages.Polish;
  7. lngBtn.setText(&quot;pl&quot;);
  8. //Set android:drawableLeft=&quot;@drawable/ic_poland&quot;
  9. }
  10. else if (currentLanguage == Languages.Polish)
  11. {
  12. currentLanguage = Languages.English;
  13. lngBtn.setText(&quot;eng&quot;);
  14. ///Set android:drawableLeft=&quot;@drawable/ic_united_kingdom&quot;
  15. }
  16. }

xml file

  1. &lt;Button
  2. android:onClick=&quot;LanguageButtonClick&quot;
  3. android:id=&quot;@+id/languageButton&quot;
  4. android:layout_width=&quot;wrap_content&quot;
  5. android:layout_height=&quot;wrap_content&quot;
  6. android:layout_marginTop=&quot;16dp&quot;
  7. android:layout_marginRight=&quot;16dp&quot;
  8. android:width=&quot;125dp&quot;
  9. android:height=&quot;45dp&quot;
  10. android:background=&quot;@drawable/language_button&quot;
  11. android:drawableLeft=&quot;@drawable/ic_united_kingdom&quot; &lt;------------------This one
  12. android:text=&quot;@string/eng&quot;
  13. android:textSize=&quot;18sp&quot;
  14. android:textStyle=&quot;bold&quot;
  15. app:backgroundTint=&quot;#FFFFFF&quot;
  16. app:backgroundTintMode=&quot;multiply&quot;
  17. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  18. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

答案1

得分: 0

  1. 您可以使用 `setCompoundDrawables` 方法
  2. ```java
  3. Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_united_kingdom);
  4. lngBtn.setCompoundDrawables(drawable, null, null, null);

以下是完整代码

  1. public void LanguageButtonClick(View view)
  2. {
  3. Button lngBtn = findViewById(R.id.languageButton);
  4. if(currentLanguage == Languages.English)
  5. {
  6. currentLanguage = Languages.Polish;
  7. lngBtn.setText("pl");
  8. Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_poland);
  9. lngBtn.setCompoundDrawables(drawable, null, null, null);
  10. }
  11. else if (currentLanguage == Languages.Polish)
  12. {
  13. currentLanguage = Languages.English;
  14. lngBtn.setText("eng");
  15. Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_united_kingdom);
  16. lngBtn.setCompoundDrawables(drawable, null, null, null);
  17. }
  18. }
英文:

You can use the setCompoundDrawables

  1. Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_united_kingdom);
  2. lngBtn.setCompoundDrawables(drawable, null, null, null);

Here's the full code

  1. public void LanguageButtonClick(View view)
  2. {
  3. Button lngBtn = findViewById(R.id.languageButton);
  4. if(currentLanguage == Languages.English)
  5. {
  6. currentLanguage = Languages.Polish;
  7. lngBtn.setText(&quot;pl&quot;);
  8. Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_poland);
  9. lngBtn.setCompoundDrawables(drawable, null, null, null);
  10. }
  11. else if (currentLanguage == Languages.Polish)
  12. {
  13. currentLanguage = Languages.English;
  14. lngBtn.setText(&quot;eng&quot;);
  15. Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_united_kingdom);
  16. lngBtn.setCompoundDrawables(drawable, null, null, null);
  17. }
  18. }

huangapple
  • 本文由 发表于 2020年10月26日 01:08:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64526512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定