2012-12-11 150 views
2

有關佈局的問題。如何以編程方式管理線性子佈局?

我想動態地添加此視圖。

邏輯代碼:

linear1 = (LinearLayout) findViewById(R.id.parent); 
    linear2 = (LinearLayout) findViewById(R.id.chiled); 

    int len = 4; 

    for (int i = 1; i <= len; i++) { 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT); 

     LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT); 

     params1.gravity = Gravity.RIGHT; 

     final int id_txt; 
     ImageView iv = new ImageView(this); 
     iv.setId(i); 
     id_txt = iv.getId(); 
     iv.setBackgroundResource(R.drawable.ic_launcher); 
     linear1.addView(iv, params); 
     iv = ((ImageView) findViewById(id_txt)); 

     for (int j = 1; j < 2; j++) { 
      final int id_; 
      Button btn = new Button(this); 
      btn.setId(i); 
      id_ = btn.getId(); 
      btn.setText("button " + id_); 
      linear2.addView(btn, params1); 
      btn = ((Button) findViewById(id_)); 

      btn.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        Toast.makeText(v.getContext(), 
          "Button clicked index = " + id_, 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
     // btn.setBackgroundColor(Color.rgb(70, 80, 90)); 

     // linear1.addView(txt, params); 

     // params.addRule(RelativeLayout.RIGHT_OF, txt.getId()); 

     iv.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Toast.makeText(view.getContext(), 
         "text clicked index = " + id_txt, 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 
       } 
    } 

XML代碼:在子視圖動態

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/parent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/chiled" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 

我想在父視圖中添加圖像和兩個按鈕。

我激勵,使這種類型的視圖形式

Android heterogeneous gridview like pinterest?

應該等作爲

enter image description here

電流輸出作爲

enter image description here

我不知道問題在哪裏。

一個奇怪的問題,我現在面臨在我的編輯如果我代碼看到佈局那麼它表明我android:orientation="vertical"如果我輪廓顯示android:orientation="horizontal"爲每個佈局看。這怎麼可能 ?

幫我解決out.Thanks

回答

0

由於水平LinearLayout chiled是垂直LinearLayout parent的第一個孩子是作爲第一個項目對齊。因此,將視圖添加到它將放置在頂部。

嘗試將chiled佈局移至根LinearLayout root

+0

不,它不能。還有一些其他的資源可用。 –

+0

然後嘗試添加具有特定索引的孩子,如'getChildCount() - 1'。 – Greeny

1

編輯:

首先,通過添加按鈕到XML開始。與圖像視圖一起。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/extra_root" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

    <LinearLayout 
     android:id="@+id/chiled" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Like" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Dislike" /> 
    </LinearLayout> 

</LinearLayout> 

然後,您可以刪除添加視圖的任何概念,因爲它們已經存在。

package com.example.yul; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class extra extends Activity { 
    Button like, dislike; 
    LinearLayout root, sub; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.extra); 

    like = (Button) findViewById(R.id.button1); 
    dislike = (Button) findViewById(R.id.button2); 

    // add button listeners here. 

    ImageView iv = (ImageView) findViewById(R.id.image); 

     iv.setBackgroundResource(R.drawable.ic_launcher); 

    } 
    } 
} 

這將只顯示一個圖像,但按鈕。

你需要做的是應用這個xml,並將代碼應用到gridView或類似的。因爲否則會發生ID衝突。

+0

所以你說的是動態地創建一個佈局呢?其實這是我的主要問題http://stackoverflow.com/questions/13796618/how-to-manage-control-in-relative-layout-dynamically但我分開,所以它可以很容易找出解決方案。這是單一的垂直內膽佈局,我需要再添加兩個,因此它看起來像畫廊一樣。希望你明白我的理念。 –

+0

@chintankhetiya它看起來像你的情況中最簡單的方式,所以它會動態增長。其餘的將以相同的方式運作。又名,根仍將被添加到屏幕上的同一個莊園。只要記住在您動態創建的線性佈局上設置正確的佈局。我假設你想要它爲其中一個水平。試一試,看看你的想法。 – Doomsknight

+0

你有工作在這種類型的畫廊?我的理念是好還是不好? –

相關問題