2014-07-18 3 views
-2

프로그래밍 방식으로 하나의 레이아웃을 생성하고 레이아웃에서 하나의 textview 및 edittext를 생성하고 싶습니다. 어떻게 보이게 할 수 있을까요?프로그래밍 방식으로 안드로이드 레이아웃 생성

enter image description here

이 코드이지만 작업 :(

RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.mRlayout); 
RelativeLayout.LayoutParams mRparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
EditText myEditText = new EditText(context); 
myEditText.setLayoutParams(mRparams); 
mRlayout.addView(myEditText); 
+0

에 선형 레이아웃의 객체를 생성 새로운 레이아웃을 만드는 방법 ?? –

+2

우리는 숙제를하지 않을 것입니다. – luiscosta

+1

하시겠습니까? 그렇다면 그것을 할 자유롭게 ... – PKlumpp

답변

0

이 당신이 당신의 문제를 해결하는 데 도움이되기를 바랍니다,이 방법을 시도를 일부러.

public class MainActivity extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     LinearLayout layout = new LinearLayout(this); 
     layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     TextView textView = new TextView(this); 
     textView.setText("TextView"); 
     layout.addView(textView); 
     EditText editText = new EditText(this); 
     editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1f)); 
     editText.setHint("EditText"); 
     layout.addView(editText); 
     setContentView(layout); 
    } 
} 
+0

문제는 내 클래스의 setContentView dident 작업입니다. 이 클래스는 활동이나 대화가 아니라 .. 그것은 일부 요소에 대한 이중 부분 것입니다하지만 그것을 사용하여 n 요소를 생성하고 싶습니다 – user3568005

0

나는 것 layout.xml 내에서 LinearLayout을 정의하고 Java로 객체를 만든 다음 LinearLayout을 textview로 추가하는 것이 좋습니다.

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

자바

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    LinearLayout cat_linear=(LinearLayout) findViewById(R.id.list_Category); 

    TextView tv = new TextView(context); 
    tv.setText("This is Text View"); 
    tv.setLayoutParams(new LinearLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f)); 
    cat_linear.addView(tv); 


    EditText ed = new EditText (context); 
    ed.setHint("EditText"); 
    ed.setLayoutParams(new LinearLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f)); 
    cat_linear.addView(ed); 
} 
1

먼저

<LinearLayout 
     android:id="@+id/horizantalLinear" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horiontal" > 
</LinearLayout> 

같은 XML 파일에 선형 레이아웃을 추가 한 다음 당신이 원하는 무엇을 당신의 자바 코드

EditText myEditText = new EditText(this); // Pass it an Activity or Context 
    LayoutParams editLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,  LayoutParams.WRAP_CONTENT);    
    myEditText.setLayoutParams(editLayoutParams); 

    myEditText.setVerticalFadingEdgeEnabled(true); 
    myEditText.setHint(hint); 
    myEditText.setId(Integer.parseInt(id)); 
    myLayout.addView(myEditText);// myLayout is object of linear layout created in xml file 
관련 문제