한 번에 하나의 버튼을 클릭하고 기존 RelativeLayout에 여러 TextView를 추가하고 TextView를 서로 겹치지 않고 프로그래밍 방식으로 목표하는 것이 목표입니다.
여기가 마침내 나왔습니다.이 방법이 효과적 일 수는 있지만 최선의 방법 (또는 좋은 방법)이라면 확실하지 않습니다. 명심해야 할
// Creates a variable to keep track of the amount of TextViews, the
// first TextView, an array, and then stores the TextView in the Array
int numberOfTextViews = 1;
TextView[] textViewArray = new TextView[20];
TextView textViewToSeeFirst = (TextView) findViewById(R.id.textView1);
textViewArray[numberOfTextViews - 1] = textViewToSeeFirst;
// Also need to reference the RelativeLayout we are putting TextViews in
RelativeLayout rLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lparams.addRule(RelativeLayout.RIGHT_OF, textViewArray[numberOfTextViews - 1].getId());
lparams.addRule(RelativeLayout.ALIGN_TOP, textViewArray[numberOfTextViews - 1].getId());
TextView newTextView = new TextView(TheActivityYouAreUsingActivity.this);
newTextView.setText("text you want");
newTextView.setId(numberOfTextViews);
textViewArray[numberOfTextViews] = newTextView;
rLayout1.addView(textViewArray[numberOfTextViews], lparams);
numberOfTextViews = numberOfTextViews + 1;
}
어떤 것들은 :
다음 코드에서 onCreate() 메소드 내부에 존재하는 RelativeLayout.LayoutParams에 대한
매개 변수가 중요하다, developer material on these parameters 참조 . WRAP_CONTENT는 TextViews가 전체 부모가 아닌 텍스트의 크기 만 차지하기 때문에 필자의 요구에 맞게 선택되었습니다 ... 내가 이것을 변경하기 전에 겹침이 발생했습니다.
각각의 새로운 텍스트 뷰의 id는 새로운 레이아웃 나중에 참조 할 경우 설정
는 ID가 양수를해야 매개 변수 및 0 긍정적되지 않습니다해야
RelativeLayout의이 TextViews을 잡고 그들을 처리되면, textViewArray는 각각의 텍스트 뷰의 ID를 저장하고 해당 XML이 안에 그것을 위해 가고있다
필요하다면 참조 할 수있다 주 부모 : 제 RelativeLayout의 및 텍스트 뷰 모두 ID를 가지고
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".2" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/die_one" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name_a_button" />
</RelativeLayout>
알림이 때문에이를 참조 할 수있는 활동에 addRule() 방법이다.
이 코드를 사용하면 버튼을 클릭하고 RelativeLayout에 새로운 TextViews를 겹치지 않고 추가 할 수 있습니다.
스택 오버플로는 프로그래밍 관련 질문입니다. 귀하의 질문은 무엇인가? FAQ 스타일의 항목을 작성하려고하면 "질문"의 모든 내용이 답변에 포함되어야하며 질문은 문제를 설명하는 실제 스택 오버 플로우 관련 질문이어야합니다. – CommonsWare
btw - CommonsWare의 의견이 유효합니다. 내 원래 게시물은 질문을 직접 충분히 묻지 않았다. 이후 게시물을 편집했다 ... – probability