2012-12-11 3 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> 
내가 부모보기에 이미지를 추가 할

두 개의 버튼을 누릅니다.

I는

Android heterogeneous gridview like pinterest?

그것은 같아야 이러한 타입 뷰 형태를 만들기 위해 고무

같은

enter image description here

전류 출력

0,123,516로서

enter image description here

어디에 문제가 있는지 알 수 없습니다. 내가 코드에서 레이아웃을 볼 수 경우

이상한 문제를 내가 각 레이아웃 android:orientation="horizontal"을 보여줍니다 개요에서 볼 경우 다음의 나에게 android:orientation="vertical"를 보여줍니다 내 편집기 지금 직면하고있다. 그게 어떻게 가능해 ? 저를 탈 수평 LinearLayout chiled 이후

답변

0

를 해결하기

도움말은 첫 번째 항목으로 정렬되고 수직 LinearLayout parent의 첫 번째 자식이다. 따라서 뷰를 추가하면 맨 위에 배치됩니다.

루트 LinearLayout rootchiled 레이아웃을 이동하십시오.

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

    } 
    } 
} 

버튼 하나만 표시됩니다. 당신이해야 할 일은

gridView 또는 유사한이 XML 및 코드를 적용합니다. 그렇지 않으면 ID 충돌이 발생합니다.

+0

그래서 동적으로 레이아웃을 생성한다고 말하고 있습니까? 사실 이것은 내 주요 질문입니다 http://stackoverflow.com/questions/13796618/how-to-manage-control-in-relative-layout-dynamically하지만 솔루션을 쉽게 찾을 수 있도록 나눕니다. 이것은 하나의 수직 라이너 레이아웃으로 갤러리를 더 두 개 더 추가해야합니다. 내 개념을 얻길 바랍니다. –

+0

@chintankhetiya 귀하의 상황에서 가장 쉬운 방법 인 것처럼 보이므로 동적으로 커집니다. 나머지도 같은 방식으로 작동합니다. 일명, 루트는 여전히 같은 장원의 스크린에 추가 될 것입니다. 동적으로 생성 한 선형 레이아웃에서 정확한 오리넷을 설정하는 것을 잊지 마십시오. 나는 당신이 그것들 중 하나에 대해 수평으로 가고 싶다고 가정한다. 시도해보고 생각을 확인하십시오. – Doomsknight

+0

이 유형의 갤러리에서 작업 하시겠습니까? 내 개념은 좋지 않습니까? –

관련 문제