2016-06-10 2 views
0

내 앱은 화면 하단의 EditText과 버튼으로 구성됩니다. EditText에 뭔가를 입력하고 해당 버튼을 누르는 경우 TextView이 화면 상단에 생성됩니다.키보드 바로 위의 텍스트 뷰 추가

그러나 내가 원하는 것은 그 버튼을 누르면 TextViewEditText 필드 바로 위에 생성된다는 것입니다. 그리고 다시 그 작업을 수행 할 경우 이 EditText 바로 위에 생성되고 다른 하나는 TextView 아래에 다시 생성되어야합니다.

이것은 지금까지 제 코드입니다. (이것은 RelativeLayout 안에 모든이다 그러나 해당 코드로 감지되지 때문에 어떤 이유로 나는 ... 여기에 그것을 넣어 수 없습니다.)

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


<Button 
    android:id="@+id/button" 
    android:layout_width="100dp" 
    android:layout_height="50dp" 
    android:text="Senden" 
    android:layout_gravity="center_horizontal" 
    android:layout_alignBottom="@+id/editText" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

<EditText 
    android:layout_width="470px" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

그리고 내 활동 : 여기

는 XML의

public class MainActivity extends AppCompatActivity { 

private Button button; 
private EditText editText; 
private LinearLayout linearLayout; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    linearLayout = (LinearLayout) findViewById(R.id.linearLayout); 
    editText = (EditText) findViewById(R.id.editText); 
    button = (Button) findViewById(R.id.button); 

    final View.OnClickListener btnHandler = new View.OnClickListener() { 
     public void onClick(View v) { 
      String msg = editText.getText().toString(); 
      editText.getText().clear(); 
      linearLayout.addView(createNewTextView(msg)); 
      View view = getCurrentFocus(); 
      if(view != null) { 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
      } 
     } 
    }; 

    button.setOnClickListener(btnHandler); 
} 

private TextView createNewTextView(String text) { 
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    final TextView textView = new TextView(this); 
    textView.setLayoutParams(lparams); 
    textView.setTextColor(Color.BLACK); 
    textView.setText(text); 

    return textView; 
} 

내가 원하는대로 변경하려면 무엇을해야합니까?

답변

1

android:gravity="center|bottom" 부모보기로 LinearLayout을 사용하십시오.

이제 텍스트 뷰를 추가하는 동안 새로 작성된 텍스트 뷰를 위치에 삽입하십시오.

parentLinearLayout.addView(childtextview, index); //index represents it position in linearlayout,according to you index is always 2. 
+0

LinearLayout을 부모 뷰로 사용한다고 말하면 RelativLayout을 LinearLayout으로 바꾸고 편집 텍스트와 버튼을 안에 넣어야한다는 것을 의미합니까? 그것? – nummer92

+0

예, 절대적으로, Edittext 및 Button은 처음부터 두 개의 인덱스를 취할 것이므로 매번 textview를 3 –

+0

으로 설정하십시오. 그래서 나는 말해야 할 것 같아요 : linearLayout.addView (button, 1) and linearLayout.addView (edittext, 1). 내 단추가 상단에없고 edttext가 하단에 있기 때문에 – nummer92

0

많은보기에 대해 좋은 컨트롤이 있습니다 : RecyclerView. 텍스트 뷰를 처리 할 경우에는 을 사용하십시오.

관련 문제