407 java android android-layout android-2.2-froyo
我正在动态创建按钮.我首先使用XML设置它们,我正在尝试使用下面的XML并使其编程.
<Button
android:id="@+id/buttonIdDoesntMatter"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="buttonName"
android:drawableLeft="@drawable/imageWillChange"
android:onClick="listener"
android:layout_width="fill_parent">
</Button>
Run Code Online (Sandbox Code Playgroud)
这就是我到目前为止所拥有的.除了可绘制之外,我可以做任何事情.
linear = (LinearLayout) findViewById(R.id.LinearView);
Button button = new Button(this);
button.setText("Button");
button.setOnClickListener(listener);
button.setLayoutParams(
new LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT
)
);
linear.addView(button);
Run Code Online (Sandbox Code Playgroud)
Var*_*run 984
您可以使用该setCompoundDrawables方法执行此操作.请参阅此处的示例.我使用它而不使用它setBounds并且它起作用.你可以尝试任何一种方式.
更新:在此处复制代码会导致链接断开
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );
Run Code Online (Sandbox Code Playgroud)
要么
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
txtVw.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
Run Code Online (Sandbox Code Playgroud)
要么
txtVw.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
Jig*_*iya 89
你也可以尝试一下
txtVw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
swa*_*aha 13
对我来说,它有效:
button.setCompoundDrawablesWithIntrinsicBounds(com.example.project1.R.drawable.ic_launcher, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
ami*_*phy 12
Kotlin Version使用以下代码片段向按钮添加一个可绘制对象:
val drawable = ContextCompat.getDrawable(context, R.drawable.ic_favorite_white_16dp)
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
Run Code Online (Sandbox Code Playgroud)
。
Important Point in Using Android Vector Drawable当您使用可绘制的android vector并希望向21以下的API具有向后兼容性时,请将以下代码添加到:
在应用程序级别build.gradle:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Run Code Online (Sandbox Code Playgroud)
在应用程序类中:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
}
}
Run Code Online (Sandbox Code Playgroud)
gng*_*ath 11
myEdtiText.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley,0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
如果您使用drawableStart、drawableEnd、drawableTop或drawableBottom;您必须使用“ setCompoundDrawablesRelativeWithIntrinsicBounds ”
edittext.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.anim_search_to_close, 0)
Run Code Online (Sandbox Code Playgroud)
为我工作。在右侧设置drawable
tvBioLive.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_close_red_400_24dp, 0)
Run Code Online (Sandbox Code Playgroud)
我这样做了:
// Left, top, right, bottom drawables.
Drawable[] drawables = button.getCompoundDrawables();
// get left drawable.
Drawable leftCompoundDrawable = drawables[0];
// get new drawable.
Drawable img = getContext().getResources().getDrawable(R.drawable.ic_launcher);
// set image size (don't change the size values)
img.setBounds(leftCompoundDrawable.getBounds());
// set new drawable
button.setCompoundDrawables(img, null, null, null);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
235558 次 |
| 最近记录: |