1

我有自定义类正在扩展ViewGroup类。在里面我想添加一些视图,包括两个必须相邻的按钮。我想出了用这个视图创建 XML 文件然后使用 addView 添加它的想法。不幸的是,它没有成功。

我认为更好的第二个想法是以编程方式创建 LineraLayout 以及两个按钮,设置所有设置然后添加。

这是代码:

//adding view to view group
addView(createView(ctx));

//function creating linearlayout and buttons
private View createView(Context ctx){
        LinearLayout l = new LinearLayout(ctx);
        l.setOrientation(LinearLayout.HORIZONTAL);
        l.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        Button btnL = new Button(ctx);
        btnL.setText("Text1");
        Button btnR = new Button(ctx);
        btnR.setText("Text2");
        l.addView(btnL);
        l.addView(btnR);
        return l;
    }

问题是,我现在根本看不到这种观点。我的意思是这是从 LinearLayout 创建的。LogCat 中没有错误。

有人可以告诉我我必须做什么才能添加它吗?

编辑:

这是代码onLayout,我没有onMeasure

@Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int w = getWidth();
        int h = getHeight();
        for (int i = 0; i < NUM_CHILDREN; i++) {
            View v = getChildAt(i);
            int ii = i * 4;
            float fl = w * COORDS[ii] / GRID_WIDTH;
            float ft = h * COORDS[ii + 1] / GRID_HEIGHT;
            float fr = fl + w * COORDS[ii + 2] / GRID_WIDTH;
            float fb = ft + h * COORDS[ii + 3] / GRID_HEIGHT;
            v.layout(Math.round(fl), Math.round(ft), Math.round(fr),
                    Math.round(fb));
        }
    }

一般来说,我只是使用巨大的表格,在那里我可以获得给定视图的确切尺寸应该是什么信息。

4

2 回答 2

4

像这样覆盖您的自定义 ViewGroup 的 onMeasure():

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int w = getWidth();
    int h = getHeight();
    for (int i = 0; i < NUM_CHILDREN; i++) {
        View v = getChildAt(i);
        int ii = i * 4;
        float fw = w * COORDS[ii+2] / GRID_WIDTH;
        float fh = h * COORDS[ii+3] / GRID_HEIGHT;
        int wSpec = MeasureSpec.makeMeasureSpec(Math.round(fw), MeasureSpec.EXACTLY);
        int hSpec = MeasureSpec.makeMeasureSpec(Math.round(fh), MeasureSpec.EXACTLY);
        Log.d(TAG, "onMeasure Width " + MeasureSpec.toString(wSpec) + ", Height " + MeasureSpec.toString(hSpec));
        v.measure(wSpec, hSpec);
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
于 2013-07-14T07:05:42.803 回答
0

对于您的第一个想法,我将按照您的要求为您提供我项目中的示例。

它基于一个简单的想法。我有一个在 xml 布局文件中设计的元素。我从 xml 布局文件加载它。我改变了一些东西,最后我将它添加到当前正在屏幕上查看的元素中。

                RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.android_messenger_sent_message, null);
                TextView inbox_message = (TextView)relativeLayout.findViewById(R.id.sentMessage);
                inbox_message.setText(conversationInfo.getBody());

                TextView inbox_messageStatus = (TextView) relativeLayout.findViewById(R.id.sentMessageStatus);
          String dateStatus = getDateForStatus(conversationInfo.getDateSent());
          inbox_messageStatus.setText(""+dateStatus+" "+getString(R.string.me));

                linearLayoutGlobal.addView(relativeLayout,0);

来自下面的 XML 布局文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="2dp"
        android:src="@drawable/contact_ico" />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginRight="5dp"
        android:layout_toRightOf="@+id/imageView1"
        android:background="@color/light_blue"
        android:orientation="vertical">
        <TextView 
           android:id="@+id/sentMessageStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="12:59, FirstName LastName"
            android:textColor="@android:color/darker_gray"
            android:paddingBottom="5dp"/>
        <TextView
            android:id="@+id/sentMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a long sample message and what if it will be toooooooo long?"
            />
    </LinearLayout>

</RelativeLayout>
于 2013-07-13T20:17:04.017 回答