마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

textView 위치가 달라집니다...

0 추천

fragment.xml에는 textView의 위치를 다음과 같이 설정하였습니다.

그런데 실제 작동을 할 때는 다음과 같은 곳에 나옵니다.

왜그럴까요...

스크롤 적용하기 위해서 xml에서는

android:scrollbars = "vertical|horizontal"

프래그먼트에서는 

textView.setMovementMethod(new ScrollingMovementMethod()); 넣었습니다.

Sprite_ZERO (470 포인트) 님이 2022년 2월 21일 질문

1개의 답변

0 추천
 
채택된 답변

프레그먼트의 레이아웃이 ConstraintLayout으로 되어 있으신 것 같네요. ConstraintLayout은 constraint를 설정해줘야 제대로 동작하는 레이아웃입니다. 레이아웃 디자이너에 보이는 것은 디자인시에 보이는 레이아웃일 뿐 실제로 앱이 실행될 때 보여질 레이아웃과 동일하지 않을 수 있습니다. ConstraintLyoust에서 tools:* 로 되어있는 속성을 지워보시면 확인이 됩니다.

님의 디자인으로 봤을 때 해결방법은 두가지 입니다. 하나는 ConstraintLayout에 위, 아래, 시작, 종료 constraint 를 설정하는 것이고 다른 하나는 LinearLayout에 oriendtation을 vertical로 주는 겁니다.

ConstraintLayout을 사용했을 때는 아래처럼 사용하시면 됩니다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginVertical="40dp"
        tools:text="@tools:sample/lorem/random"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

constraint는 layout_constraint<시작>_<끝>Of   이런식으로 속성명이 결정됩니다. 최소한 top과 start에 해당하는 constraint가 두개는 지정이 되어야 하며, 없으면 0, 0 지점으로 점핑을 하게 됩니다.

따라서 app:layout_constraintStart_toStartOf="parent"는 start_start  이므로 시작지점을 나타냅니다.
레이아웃의 시작점을 나타냅니다. parent는 화면의 좌측시작점을 이라고 보면 됩니다.

end_end는 끝 top_top은 최상단 bottom_bottom 최하단.

top_bottom contrint 는 나의 top이 상대방의 bottom에 위치한 걸 말합니다. bottom_top은 그 반대이구요.

기본적인  constraint 는 조금만 사용해 보시면 간단합니다.

 

그리고 님의 레이아웃의 경우는 LinearLayout + vertical이 ConstraintLayout보다 성능이 더 좋을 것 같습니다.

 

spark (227,470 포인트) 님이 2022년 2월 21일 답변
Sprite_ZERO님이 2022년 2월 21일 채택됨
정말 감사드립니다.
혹시 이것과 별개로 파일 관련 질문 하나만 드려도 될까요?
제가 GPS에서 받은 데이터를 internal storage에 txt 파일로 저장해서 보려고 하는데, 파일 저장이 제대로 되지 않는 것 같아서요...

String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                    String contents = "Log 생성 : "+now+"\n" + message;

                    String inputData = contents;

                    try {
                        FileOutputStream fos = getContext().openFileOutput("data.txt", Context.MODE_APPEND);

                        PrintWriter writer = new PrintWriter(fos);
                        writer.println(inputData);
                        writer.close();

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }

이렇게 작성하였고, 컴퓨터 내 pc에서 해당 앱 아래의 files 폴더에 아무것도 저장되지 않는데 제가 저장 위치를 잘못 알고있는걸까요?
이건 질문을 새로 올리시면 어떨까요? 다른 분들도 참고하기가 쉬울 것 같은데요.
...