내 앱은 화면 하단의 EditText
과 버튼으로 구성됩니다. EditText
에 뭔가를 입력하고 해당 버튼을 누르는 경우 TextView
이 화면 상단에 생성됩니다.키보드 바로 위의 텍스트 뷰 추가
그러나 내가 원하는 것은 그 버튼을 누르면 TextView
이 EditText
필드 바로 위에 생성된다는 것입니다. 그리고 다시 그 작업을 수행 할 경우 이 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;
}
내가 원하는대로 변경하려면 무엇을해야합니까?
LinearLayout을 부모 뷰로 사용한다고 말하면 RelativLayout을 LinearLayout으로 바꾸고 편집 텍스트와 버튼을 안에 넣어야한다는 것을 의미합니까? 그것? – nummer92
예, 절대적으로, Edittext 및 Button은 처음부터 두 개의 인덱스를 취할 것이므로 매번 textview를 3 –
으로 설정하십시오. 그래서 나는 말해야 할 것 같아요 : linearLayout.addView (button, 1) and linearLayout.addView (edittext, 1). 내 단추가 상단에없고 edttext가 하단에 있기 때문에 – nummer92