2013-07-13 32 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