Java Android-在代码中应用9补丁
如何在代码中将9补丁应用于Android TextView 你可能会问,为什么?因为我需要动态设置控件的数量 我正在尝试做类似的事情,但它不起作用: (代码中我的9补丁“notif\u bubble\u white.9.png”是R.drawable.notif\u bubble\u white) 其中,lpL定义为:Java Android-在代码中应用9补丁,java,android,nine-patch,Java,Android,Nine Patch,如何在代码中将9补丁应用于Android TextView 你可能会问,为什么?因为我需要动态设置控件的数量 我正在尝试做类似的事情,但它不起作用: (代码中我的9补丁“notif\u bubble\u white.9.png”是R.drawable.notif\u bubble\u white) 其中,lpL定义为: LinearLayout.LayoutParams lpL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams
LinearLayout.LayoutParams lpL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
您可以一次创建所有控件,然后使用
control.setVisibility(View.GONE)
隐藏它们,直到需要为止;或者,如果控件的数量未知,您可以膨胀布局文件,如中所述
从参考问题复制的代码(计入Cedric或Pentium10、idk,并假设控件是一个按钮):
button.xml:
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
“我如何在代码中将9补丁应用于Android TextView?”他提出了一个问题,在XML代码中,您可以添加9patch背景,充气器将始终正确充气9patch
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
button = (Button) getLayoutInflater().inflate(R.layout.button, null);