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

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

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(&quot;pl&quot;);
        //Set android:drawableLeft=&quot;@drawable/ic_poland&quot;
    }
    else if (currentLanguage == Languages.Polish)
    {
        currentLanguage = Languages.English;
        lngBtn.setText(&quot;eng&quot;);
        ///Set android:drawableLeft=&quot;@drawable/ic_united_kingdom&quot;
    }
}

xml file

&lt;Button
    android:onClick=&quot;LanguageButtonClick&quot;
    android:id=&quot;@+id/languageButton&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginTop=&quot;16dp&quot;
    android:layout_marginRight=&quot;16dp&quot;
    android:width=&quot;125dp&quot;
    android:height=&quot;45dp&quot;
    android:background=&quot;@drawable/language_button&quot;
    android:drawableLeft=&quot;@drawable/ic_united_kingdom&quot; &lt;------------------This one
    android:text=&quot;@string/eng&quot;
    android:textSize=&quot;18sp&quot;
    android:textStyle=&quot;bold&quot;
    app:backgroundTint=&quot;#FFFFFF&quot;
    app:backgroundTintMode=&quot;multiply&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

答案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(&quot;pl&quot;);
        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(&quot;eng&quot;);
        Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_united_kingdom);
    lngBtn.setCompoundDrawables(drawable, null, null, null);
    }
}

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:

确定