[英]Setting Nested TextView Text
I've created a layout for a button format I use multiple times. 我已经为多次使用的按钮格式创建了一个布局。 The button format has a TextView and an ImageView.
按钮格式具有TextView和ImageView。 With the way I'm including this layout in my main activity, I don't think I'm able to change the text of the inner TextView dynamically in Java or in the XML.
通过将这种布局包含在我的主要活动中的方式,我认为我无法用Java或XML动态更改内部TextView的文本。 Is there a different way I can do this such that I can set the text of the inner TextView?
我可以通过其他方式设置内部TextView的文本吗?
Layout: 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/settingslayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="13dp"
android:textSize="20dp"
android:text="CHANGE ME"
android:id="@+id/text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="25dp"
android:layout_alignParentRight="true"
android:src="@drawable/left"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:maxHeight="30dp"
android:maxWidth="30dp"/>
</RelativeLayout>
Main Activity: 主要活动:
...
<include layout="@layout/settings_layout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/accountStaticUnderline"
android:id="@+id/termBegin"
android:text="TEST" /> //DOESNT WORK
...
</RelativeLayout>
当然,只需在您的活动中键入此代码
((TextView)findViewById(R.id.text)).setText("enter_text_here");
Setting the text that way won't work because android:text
is not applicable to the <include>
tag. 以这种方式设置文本将不起作用,因为
android:text
不适用于<include>
标签。 More specifically, it's not applicable to the thing being included, which is a RelativeLayout
. 更具体地说,它不适用于所包含的东西,它是
RelativeLayout
。 (You couldn't put the android:text
on the RelativeLayout
and have it apply to the TextView
, nor would you expect that to work.) (您无法将
android:text
放在RelativeLayout
,并不能将其应用于TextView
,也不希望它能正常工作。)
My first suggestion (and the easiest immediate solution) is to use TextView
's built in support for compound drawables so you can simply use TextView
s instead of include
s and have a style resource for the attributes you want. 我的第一个建议(也是最简单的即时解决方案)是使用
TextView
对复合可绘制对象的内置支持,因此您可以简单地使用TextView
而不是include
并拥有所需属性的样式资源。
If that's not good enough for your use case, then you might need to make a custom View. 如果这还不足以满足您的用例,那么您可能需要制作一个自定义视图。 This view will replace the
RelativeLayout
and have the TextView
and ImageView
as children. 此视图将替换
RelativeLayout
并将TextView
和ImageView
作为子级。 The main thing to decide is where and how the children are created and their references are obtained: you can create them manually in Java when the parent is being constructed; 要决定的主要事情是在何处以及如何创建子级以及如何获得它们的引用:在构造父级时,可以使用Java手动创建它们; or you can use some combination of layouts with
<include>
s and/or <merge>
s. 或者,您可以将布局与
<include>
和/或<merge>
。 Making the text attribute work then requires some use of a <declare-styleable>
. <declare-styleable>
text属性起作用,就需要使用<declare-styleable>
。
I assume you want the children to always appear the same and that you want to reuse the layout you already made (ie you don't want to set all the attributes manually), so this is what I would probably do: 我假设您希望子级始终显示相同,并且希望重用已经完成的布局(即,您不想手动设置所有属性),所以我可能会这样做:
public class MyButton extends RelativeLayout {
private TextView mTextView;
private ImageView mImageView;
public MyButton(Context context) {
super(context);
init(context, null);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attributeSet) {
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.my_button, this, true); // used with merge tag
mTextView = (TextView) findViewById(R.id.text);
mImageView = (ImageView) findViewById(R.id.image);
int[] attrs = {android.R.attr.text};
TypedArray a = context.obtainStyleAttributes(attributeSet, attrs);
String text = a.getString(0, null);
mTextView.setText(text);
a.recycle();
}
}
In res/layout/my_button.xml
: 在
res/layout/my_button.xml
:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="13dp"
android:textSize="20dp"
android:text="CHANGE ME"
android:id="@+id/text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginRight="25dp"
android:layout_alignParentRight="true"
android:src="@drawable/left"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:maxHeight="30dp"
android:maxWidth="30dp"/>
</merge>
In your activity layout: 在您的活动布局中:
...
<!-- no more include -->
<com.package.MyButton
android:id="@+id/something"
android:layout_width="..."
android:layout_height="..."
android:text="..." />
<!-- you can have multiple instances with different IDs -->
<com.package.MyButton
android:id="@+id/something_else"
android:layout_width="..."
android:layout_height="..."
android:text="..." />
...
You could also use your own <declare-styleable>
for the custom view, which will be necessary if later you want to have custom XML attributes for it, but the approach above should be sufficient. 您也可以对自定义视图使用自己的
<declare-styleable>
,如果以后要为其具有自定义XML属性,则很有必要,但是上述方法就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.