Sending text from fragment to custom view where I want to use canvas.drawText [duplicate]

浪子不回头ぞ 提交于 2019-12-11 18:27:04

问题


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.

What is the procedure for this?

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;
    }
}

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);
....
}

...and what I want is to get myString from Fragment1.java to the View1.java.


回答1:


You could create the com.example.stuff.View1 custom view dynamically, i.e. dont add it in the xml but add it using the code. And then you could pass the text in the constructor of the com.example.stuff.View1.
Other way could be to create a method in the com.example.stuff.View1 class and set the text there. For example

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();
    }

    public void setMyText(String string) {
        myString=string;
    }
...
canvas.drawText(myString, margin1, margin2, paint);
....
}

then in your code do something like this

View view = inflater.inflate(R.layout.fragment_layout, container,false);
    txtBox = (TextView) view.findViewById(R.id.TxtBox);
    String myString = "testing";
    txtBox.setMyText(myString);



回答2:


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).



来源:https://stackoverflow.com/questions/14890491/sending-text-from-fragment-to-custom-view-where-i-want-to-use-canvas-drawtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!