gpt4 book ai didi

java - Android - 在代码中应用 9 补丁

转载 作者:行者123 更新时间:2023-12-01 05:28:45 24 4
gpt4 key购买 nike

如何在代码中将 9 补丁应用到 Android TextView?

你可能会问为什么?因为我需要动态设置控件的数量。

我尝试做类似的事情但它不起作用:

(代码中我的 9-patch “notif_bubble_white.9.png”是R.drawable.notif_bubble_white)

  tv = new TextView(this); 
tv.setText("Test Message");
tv.setLayoutParams(lpL);
tv.setPadding(0, 0, paddingValue, 0);
CustomFonts.ApplyFont_Custom1(tv, context);
tv.setBackgroundResource(R.drawable.notif_bubble_white);
// tv.setTextColor(TxtColFromCl);
layout.addView(tv);

其中 lpL 定义为:

  LinearLayout.LayoutParams lpL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
LinearLayout.LayoutParams.WRAP_CONTENT);

最佳答案

您可以一次性创建所有控件,并在需要时使用 control.setVisibility(View.GONE) 隐藏它们,或者,如果控件数量未知,您可以膨胀布局文件,如 this question 中所述.

从引用问题复制的代码(归功于 Cedric、或 Pentium10、idk,并假设控件是一个按钮):

按钮.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" />

充气按钮:

button = (Button) getLayoutInflater().inflate(R.layout.button, null);

关于java - Android - 在代码中应用 9 补丁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9352285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com