2013-07-13 31 views
1

我已经定制了扩展ViewGroup类的类。在它里面,我想添加一些视图,包括两个必须相邻的按钮。我想出了使用这个视图创建XML文件的想法,然后使用addView添加它。不幸的是它不能解决问题。将LinearLayout添加到ViewGroup

我认为更好的第二个想法是创建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)); 
     } 
    } 

总的来说,我只是用巨大的表,其中我有什么样的信息给出确切尺寸应该是视角。

+0

这将有助于看到代码为您定制'ViewGroup'的'onMeasure'和'onLayout'方法。 – Luksprog

+0

@Luksprog刚刚添加。 – sebap123

+0

为什么你首先从xml加载两个按钮的想法不起作用? –

回答

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); 
} 
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> 
+0

你能否也展示一下,你是如何创建'linearLayoutGlobal'的,因为我认为存在一个我正在与之战斗的问题。第二件事 - 我想你的代码是在Activity中的,因为我使用扩展ViewGroup的类,这是没用的。 – sebap123