从片段发送文本自定义视图,我想用canvas.drawText [英] Sending text from fragment to custom view where I want to use canvas.drawText
问题描述
我是新来的Android,并希望从碎片一些文本传递到自定义视图。里面的自定义视图我想用 canvas.drawText
来的字符串中的位置。我想用其原因 canvas.drawText
是某些图形的位置一致。
I am new to Android and want to pass some text from a fragment to a custom view. Inside the custom view I want to use canvas.drawText
to position the string. The reason I want to use canvas.drawText
is for consistency with the position of some graphics.
什么是程序呢?
只是为了澄清,我的片段有这样的事情:
Just to clarify, my fragment has something like this:
public class Fragment1 extends Fragment {
private TextView view;
private TextView txtBox;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container,false);
txtBox = (TextView) view.findViewById(R.id.TxtBox);
String myString = "testing";
txtBox.setText(myString);
return view;
}
}
我为视图1(我的自定义视图)一个fragment_layout.xml文件:
I have a fragment_layout.xml file for View1 (my custom view):
<com.example.stuff.View1
android:id="@+id/TxtBox"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我想调用,使自定义视图中:
The call I am wish to make inside the custom view:
public class View1 extends TextView {
//constructors:
public View1(Context context, AttributeSet ats, int ds) {
super(context, ats, ds);
init();
}
public View1(Context context) {
super(context);
init();
}
public View1(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
...
canvas.drawText(myString, margin1, margin2, paint);
....
}
...和我要的是从Fragment1.java获得的myString
到View1.java。
谢谢!
...and what I want is to get myString
from Fragment1.java to the View1.java.
Thanks!
推荐答案
我能够用我收到的答案来解决问题的here (使用的setter / getter方法)和<一个href=\"http://stackoverflow.com/questions/14913973/nullpointerexception-when-accessing-relativelayout-from-a-custom-view\">here (MIS自定义布局通话摆脱导致空指针异常)。
I was able to solve the problem with the answers I received here (using setters/getters) and here (getting rid of the resulting NullPointer exception from mis-defined layout calls).
这篇关于从片段发送文本自定义视图,我想用canvas.drawText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!