나는 Activity_main 레이아웃이 비어 있습니다. 화면 너비의 일정 비율을 차지하는 단추를 추가하고 싶습니다. 이러한 이유 때문에 프로그래밍 방식으로 추가하려고합니다. 이 사이트의 모든 예제는 LinearLayout을 사용합니다. Activity_main에 직접 버튼을 추가 할 수 있습니까?안드로이드의 빈 MainActivity에 버튼을 추가 할 수 있습니까?
감사합니다.
나는 Activity_main 레이아웃이 비어 있습니다. 화면 너비의 일정 비율을 차지하는 단추를 추가하고 싶습니다. 이러한 이유 때문에 프로그래밍 방식으로 추가하려고합니다. 이 사이트의 모든 예제는 LinearLayout을 사용합니다. Activity_main에 직접 버튼을 추가 할 수 있습니까?안드로이드의 빈 MainActivity에 버튼을 추가 할 수 있습니까?
감사합니다.
ViewGroup은 레이아웃 및보기 컨테이너의 기본 클래스이기 때문에 ViewGroup이 필요합니다.
선형 레이아웃을 사용하지 않으려는 경우 제약 레이아웃을 사용할 수 있습니다. 제약 레이아웃은 Guidelines과 함께 제공됩니다.이 레이아웃을 사용하면 특정 비율의 화면을 측정하고 그 옆에 버튼 (또는 뷰)을 정렬 할 수 있습니다.
상기 이미지 가이드의 작동 방식의 예이다. 첫 번째는 화면의 25 %를 차지하는 수직 가이드 라인입니다 (화면을 세로로 4 개로 분할하는 것으로 생각할 수 있습니다). 두 번째는 수평 가이드 라인으로 화면의 50 %를 차지합니다 (화면을 수평으로 2 개로 분할).
내가 화면의 특정 %로이 버튼의 수를 추가 할 것은
당신은 지침이 달성 할 수있는 폭. 여기에 단지 XML을 사용하여이를 수행하는 방법은 다음과 같습니다
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chornge.myapplication.MainActivity">
<android.support.constraint.Guideline
android:id="@+id/vertical_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/vertical_guideline" />
<android.support.constraint.Guideline
android:id="@+id/horizontal_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.50" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/horizontal_guideline" />
</android.support.constraint.ConstraintLayout>
을 그리고 여기에만 자바 사용하여 동일한 효과를 만드는 방법은 다음과 같습니다
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
ConstraintLayout constraintLayout = new ConstraintLayout(this);
constraintLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(constraintLayout);
/* vertical guideline */
Guideline verticalGuide = new Guideline(this);
ConstraintLayout.LayoutParams verticalGuideParams =
new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
verticalGuideParams.guidePercent = 0.25f;
verticalGuideParams.orientation = LinearLayout.VERTICAL;
verticalGuide.setLayoutParams(verticalGuideParams);
verticalGuide.setId(View.generateViewId());
constraintLayout.addView(verticalGuide);
/* create button1 and align its left edge to the right edge of vertical guideline*/
Button button1 = new Button(this);
button1.setId(View.generateViewId());
ConstraintLayout.LayoutParams button1Params =
new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
button1Params.leftToRight = verticalGuide.getId();
button1.setLayoutParams(button1Params);
constraintLayout.addView(button1);
/* horizontal guideline */
Guideline horizontalGuide = new Guideline(this);
ConstraintLayout.LayoutParams horizontalGuideParams =
new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
horizontalGuideParams.guidePercent = 0.5f;
horizontalGuideParams.orientation = LinearLayout.HORIZONTAL;
horizontalGuide.setLayoutParams(horizontalGuideParams);
horizontalGuide.setId(View.generateViewId());
constraintLayout.addView(horizontalGuide);
/* create button2 and align its top edge to the bottom edge of horizontal guideline*/
Button button2 = new Button(this);
button2.setId(View.generateViewId());
ConstraintLayout.LayoutParams button2Params =
new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
button2Params.topToBottom = horizontalGuide.getId();
button2.setLayoutParams(button2Params);
constraintLayout.addView(button2);
}
주 :
topToTop
또는 topToBottom
또는 bottomToTop
또는 bottomToBottom
또는 leftToLeft
또는 leftToRight
또는 rightToLeft
또는 rightToRight
을 사용하여 맞 춥니 다.0.05f
, 0.333f
, 0.8f
으로, 지침에 원하는 비율을 사용할 수 있습니다.완벽하게 작동합니다. – Pizzarius
활동에는 루트보기 인 ViewGroup이 있습니다. 해당 ViewGroup에 단추를 연결해야합니다. –