Java Android Studio에서 작동하는 제약 조건을 얻는 방법

6223 단어 javaandroid
활동 중 하나에서 객체를 프로그래밍 방식으로 정의하려고 하므로 이를 수행하기 위해 Java의 ConstraintSet 클래스를 사용하고 있습니다. 그런데 애플리케이션을 실행하면 java로 제약조건을 부여한 모든 위젯이 마치 제약조건이 없는 것처럼 0,0이 됩니다. 온라인에서 찾아 보았지만 수정 사항을 찾지 못했습니다.

액티비티.자바
`

@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>

`

좋은 웹페이지 즐겨찾기