2014-12-02 1 views
-1

처음에는 MainActivity이 비어 있습니다. "plus"버튼을 클릭하면 DialogFragment이 열리고 여기에는 두 개의 버튼 ("취소"및 "계속")과 EditText이 있습니다. EditText에서 프로젝트 이름 (이 경우 프로젝트 1)을 쓸 수 있으며 "계속"을 클릭하면 런타임에 "프로젝트 1", "재생"버튼 및 TextView "hello"와 같이 배치 할 수 있습니다 아래 이미지.버튼 옆에 TextView를 배치하는 방법

TextView "hello"는 (는) 같은 줄의 "재생"버튼 옆에 있습니다. "계속"버튼의

방법 : 여기

enter image description here

내 코드입니다

@Override 
    public void onDialogPositiveClick(DialogFragment dialog) { 
     EditText editText = (EditText) dialog.getDialog().findViewById(R.id.project_name); 
     String projectName = editText.getText().toString(); 

     LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      lp1.gravity = Gravity.END; 

     Button projectButton = new Button(this); 
      projectButton.setText(projectName); 
      projectButton.setLayoutParams(lp1); 
      projectButton.setOnClickListener(projectListener); 

     LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      lp2.gravity = Gravity.START; 
      lp2.setMarginStart(10); 

     Button playButton = new Button(this); 
      playButton.setBackgroundResource(R.drawable.play_button_green); 
      playButton.setLayoutParams(lp2); 

     TextView buttonView = new TextView(this); 
      buttonView.setText("Hello"); 

     LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row); 
      linearLayout.addView(projectButton); 
      linearLayout.addView(playButton); 
      linearLayout.addView(buttonView); 
    } 

MainActivity XML 레이아웃 : 전체 레이아웃을 작성해야

<RelativeLayout 

    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 

    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/button_row" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" > 
    </LinearLayout> 


</RelativeLayout> 

동적으로 XML을 사용하는 자바 코드를 사용합니다. 누구나 아이디어가 있으십니까?

+0

ID가 button_row 인 LinearLayout에 대해 android : orientation = "vertical"을 android : orientation = "horizontal"으로 변경하십시오. –

답변

4

수평 안드로이드를 사용하여 orientation="horizontal"

LinearLayout ll = new LinearLayout(this); 
ll.setOrientation(LinearLayout.HORIZONTAL); 
ll.addView(playButton); 
ll.addView(buttonView); 

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row); 
linearLayout.addView(projectButton); 
linearLayout.addView(ll); 
+0

감사합니다. 그것은 작동합니다! – Riccardo

+0

@ 리카르도는 대답을 받아 들였습니다. tnx) – Suvitruf

+0

나는 할 수 없습니다 ... 당신의 대답을 투표하려면 15 평판이 필요합니다 – Riccardo

1

LinearLayout 내부 ButtonTextView을 넣어 : 오리엔테이션

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" > 

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

     <TextView 
      android:id="@+id/textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="textView" 
      android:layout_weight="1"/> 

    </LinearLayout> 
1

오히려 당신은 또한 하나의 버튼을 사용하여 버튼 및 텍스트보기를 사용하는 것보다.

Drawable img = getContext().getResources().getDrawable(R.drawable.smiley);  
Button playButton = new Button(this); 
playButton.setText("Hello"); 
playButton.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null); 
playButton.setLayoutParams(lp2); 

이제 왼쪽에는 텍스트와 아이콘이있는 버튼이 표시됩니다.

관련 문제