0

我有一个从视图扩展的自定义视图类,我想在活动中使用它,但它没有显示任何内容。它运行没有错误,但黑屏没有输出。我的代码如下:

public class MoveableMenuLayout extends View {


    public MoveableMenuLayout(Context context, AttributeSet attrs) {

        super(context, attrs);
        LinearLayout ll = new LinearLayout(context);
        ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        TextView tv = new TextView(context);
        tv.setText("TES_______________");   
        ll.addView(tv);

        this.invalidate();
        this.requestLayout();
    }

    public MoveableMenuLayout(Context context) {
        super(context);

        LinearLayout ll = new LinearLayout(context);
        ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        TextView tv = new TextView(context);
        tv.setText("TES_______________");   
        ll.addView(tv);

        this.invalidate();
        this.requestLayout();

    }
}

通话活动

public class TestActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MoveableMenuLayout layout = new MoveableMenuLayout(this);   
        setContentView(layout);
    }
}

我已经通过将它包含在 xml 布局中进行了尝试,但仍然没有输出。我不确定我做错了什么。我不想改变样式我希望它是一个单独的类,我知道我可以使用 xml 布局并使用 setcontentview 直接在 Activity 中设置它,但我不想那样做。谢谢

4

2 回答 2

1

Why would it- you have no data. You created a linear layout and text view, but they aren't your children. So you have no data to draw (and haven't overridden onDraw anyway).

If you want to create a custom view that holds a set of other views, you need to derive from ViewGroup, not View. You also need to add the top level subview(s) as children, not just hold them as data members. You're probably even better off deriving directly from LienarLayout or RelativeLayout, and inflating your children directly from xml.

public class MoveableMenuLayout extends ViewGroup {


public MoveableMenuLayout(Context context, AttributeSet attrs) {

    super(context, attrs);
    LinearLayout ll = new LinearLayout(context);
    ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    TextView tv = new TextView(context);
    tv.setText("TES_______________");   
    ll.addView(tv);
    addView(ll);
}

public MoveableMenuLayout(Context context) {
    super(context);

    LinearLayout ll = new LinearLayout(context);
    ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    TextView tv = new TextView(context);
    tv.setText("TES_______________");   
    ll.addView(tv);
    addView(ll);

}

}

Will probably work. You can upgrade that to definitely by deriving from LinearLayout instead. And then you wouldn't need to have an internal layout. Like this:

public class MoveableMenuLayout extends LinearLayout {


public MoveableMenuLayout(Context context, AttributeSet attrs) {

    super(context, attrs);
    TextView tv = new TextView(context);
    tv.setText("TES_______________");   
    addView(tv);
}

public MoveableMenuLayout(Context context) {
    super(context);

    TextView tv = new TextView(context);
    tv.setText("TES_______________");   
    addView(tv);

}

}

An even better way is to do it via xml

public class MoveableMenuLayout extends LinearLayout {


public MoveableMenuLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate( ourlayoutid,  this );
}

public MoveableMenuLayout(Context context) {
    super(context);

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate( ourlayoutid,  this );

}

}

and then put the children you want in an xml file. In this case, the top level tag in the xml file should be a tag, not a layout

于 2013-02-19T17:05:44.673 回答
0

尝试为 textview 设置布局参数,因为您只是为父线性布局声明布局参数,而不是为子 textview 声明它不会获得高度和宽度。

于 2013-02-19T17:12:56.373 回答