Java Android Studio에서 작동하는 제약 조건을 얻는 방법
액티비티.자바
`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
addCharacterGroupToSelection("Outsider",ConstraintSet.PARENT_ID);
addCharacterGroupToSelection("Minion",R.id.activity_team_outsider);
}
protected void addCharacterGroupToSelection(String team, int previousGroupId) {
int teamColor; int teamTitleId; int teamGroupId;
switch (team) {
case "Outsider":
teamColor = getResources().getColor(R.color.good);
break;
case "Minion":
teamColor = getResources().getColor(R.color.evil);
break;
default:
throw new IllegalStateException("Unexpected value: " + team);
}
switch (team) {
case "Outsider":
teamGroupId = R.id.activity_team_outsider;
teamTitleId = R.id.activity_team_outsider_title;
break;
case "Minion":
teamGroupId = R.id.activity_team_minion;
teamTitleId = R.id.activity_team_minion_title;
break;
default:
throw new IllegalStateException("Unexpected value: " + team);
}
ConstraintLayout characterSelectContent = findViewById(R.id.activity_characterselect_content);
ConstraintLayout teamGroup = new ConstraintLayout(this);
TextView teamTitle = new TextView(this);
teamGroup.setId(teamGroupId);
characterSelectContent.addView(teamGroup);
teamTitle.setId(teamTitleId);
teamTitle.setText(team);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { teamTitle.setTextAppearance(R.style.body_black); }
teamTitle.setTextColor(teamColor);
teamGroup.addView(teamTitle);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(characterSelectContent);
constraintSet.constrainWidth(teamGroup.getId(),ConstraintLayout.LayoutParams.MATCH_PARENT);
constraintSet.constrainHeight(teamGroup.getId(),ConstraintLayout.LayoutParams.WRAP_CONTENT);
constraintSet.connect(teamGroup.getId(),ConstraintSet.END,ConstraintSet.PARENT_ID,ConstraintSet.END,0);
constraintSet.connect(teamGroup.getId(),ConstraintSet.START,ConstraintSet.PARENT_ID,ConstraintSet.START,0);
constraintSet.connect(teamGroup.getId(),ConstraintSet.TOP,previousGroupId,ConstraintSet.TOP,0);
constraintSet.applyTo(characterSelectContent);
}
activity.xml
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"
도구:컨텍스트=".활동">
<LinearLayout
android:id="@+id/header"
android:layout_width="0dp"
android:layout_height="@dimen/header_height"
android:layout_marginStart="@dimen/left_margin"
android:layout_marginEnd="@dimen/right_margin"
android:clipToPadding="false"
android:orientation="horizontal"
android:padding="-32dp"
android:paddingLeft="-32dp"
android:paddingRight="-32dp"
android:paddingBottom="-32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/header_editionlogo"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/left_margin"
android:layout_marginTop="@dimen/top_margin"
android:layout_marginBottom="@dimen/bottom_margin"
android:layout_weight="1"
android:contentDescription="@string/trouble_brewing"
app:srcCompat="@drawable/tb_logo" />
<TextView
android:id="@+id/activity_text_select_characters"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/top_margin"
android:layout_marginEnd="@dimen/right_margin"
android:layout_marginBottom="@dimen/bottom_margin"
android:layout_weight="3"
android:text="@string/select_characters"
android:textAlignment="center"
android:textAppearance="@style/body_black"
android:textColor="@color/black"
android:textSize="34sp" />
</LinearLayout>
<ScrollView
android:id="@+id/activity_characterselect"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="@dimen/left_margin"
android:layout_marginTop="1dp"
android:layout_marginEnd="@dimen/right_margin"
android:layout_marginBottom="@dimen/bottom_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/activity_characterselect_content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
`
Reference
이 문제에 관하여(Java Android Studio에서 작동하는 제약 조건을 얻는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/tom_gxz/how-to-get-constraints-working-in-java-android-studio-3d0l텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)