반응형
선형 레이아웃에 콘텐츠를 동적으로 추가 하시겠습니까?
예를 들어 방향이 수직 인 루트 선형 레이아웃을 정의한 경우 :
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_root"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
<!-- I would like to add content here dynamically.-->
</LinearLayout>
루트 선형 레이아웃 내부에 여러 자식 선형 레이아웃 을 추가하고 싶습니다 . 각 자식 선형 레이아웃 방향은 수평 입니다. 이 모든 것들이 출력과 같은 테이블로 끝날 수 있습니다.
예를 들어 다음과 같은 자식 레이아웃이있는 루트 :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_root"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
<!-- 1st child (1st row)-->
<LinearLayout
...
android:orientation="horizontal">
<TextView .../>
<TextView .../>
<TextView .../>
</LinearLayout>
<!-- 2nd child (2nd row)-->
...
</LinearLayout>
자식 선형 레이아웃의 수와 그 내용이 매우 동적이기 때문에 프로그래밍 방식으로 루트 선형 레이아웃에 내용을 추가하기로 결정했습니다.
두 번째 레이아웃을 프로그래밍 방식으로 첫 번째 레이아웃에 추가하는 방법은 각 자식에 대한 모든 레이아웃 속성을 설정하고 자식 내부에 다른 요소를 더 추가 할 수도 있습니다.
귀하의에서 onCreate()
다음 쓰기
LinearLayout myRoot = (LinearLayout) findViewById(R.id.my_root);
LinearLayout a = new LinearLayout(this);
a.setOrientation(LinearLayout.HORIZONTAL);
a.addView(view1);
a.addView(view2);
a.addView(view3);
myRoot.addView(a);
view1
, view2
and view3
are your TextView
s. They're easily created programmatically.
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
View child = getLayoutInflater().inflate(R.layout.child, null);
layout.addView(child);
You can achieve LinearLayout cascading like this:
LinearLayout root = (LinearLayout) findViewById(R.id.my_root);
LinearLayout llay1 = new LinearLayout(this);
root.addView(llay1);
LinearLayout llay2 = new LinearLayout(this);
llay1.addView(llay2);
참고URL : https://stackoverflow.com/questions/6661261/adding-content-to-a-linear-layout-dynamically
반응형
'code' 카테고리의 다른 글
GridView 정렬 : SortDirection 항상 오름차순 (0) | 2020.11.13 |
---|---|
행 길이가 다른 다차원 배열 할당에 malloc 사용 (0) | 2020.11.13 |
Pentaho 데이터 통합 SQL 연결 (0) | 2020.11.13 |
Youtube Javascript API-관련 동영상 비활성화 (0) | 2020.11.13 |
터미널에서 단일 라인 SFTP (0) | 2020.11.13 |