Aka*_*asH 7 android android-custom-view android-layout
我创建了一个自定义布局类(扩展RelativeLayout)并将TextView作为布局的一部分.
我想将XML中声明的属性应用到我的TextView中,无论如何我可以读取android属性(不是我的自定义属性,那部分已经处理好了).
例如,在我的XML中,我将拥有:
<my.custom.MyLayout
android:layout_width="100dp"
android:layout_height="20dp"
android:text="SomeText" />
Run Code Online (Sandbox Code Playgroud)
我想读取text属性并将其应用于我的TextView(目前它正在应用于RelativeLayout),而不是创建自己的属性并读取它.
我的自定义布局是这样的:
public class MyLayout extends RelativeLayout {
private TextView textView;
public void MyLayout(final Context context, final AttributeSet attrs) {
/**Read android attributes and apply it to TextView **/
??
}
Run Code Online (Sandbox Code Playgroud)
我目前的解决方案是创建自定义属性并阅读它们,但我觉得这不是一个好的解决方案,因为我将复制声明为TextView的每个属性.
有关我当前解决方案的更多信息
我有一个名为myText的自定义属性,用于将XML中声明的文本应用于TextView.
在我的布局XML中:
myNameSpace:myText="SomeText"
Run Code Online (Sandbox Code Playgroud)
并在我的Java类中阅读:
String text= a.getString(R.styleable.MyStyleable_myText);
textView.setText(text);
Run Code Online (Sandbox Code Playgroud)
我想摆脱我的自定义属性并阅读"android:"属性.
首先,我不确定是否android:text可以进行视图扩展设置RelativeLayout.如果有可能在TextView实施中这样做:
final Resources.Theme theme = context.getTheme();
a = theme.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyleAttr, defStyleRes);
text = a.getText(attr);
Run Code Online (Sandbox Code Playgroud)
所以基本上它与获取自定义属性的方式相同,但具有不同的样式.
如果它不起作用,我会考虑另一种方法.我为我的一个自定义视图做了这个.由于您TextView在自定义视图中有一个,您可以用XML创建它,然后在自定义视图中获得对该子项的引用.
<YourCustomView>
<TextView android:text="someText"/>
<YourCustomView/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1854 次 |
| 最近记录: |