Hello,

kok nae-ga ha-myun an-dweneun MAGIC...🧚

안드로이드

CustomView 레이아웃 문제 해결

✿도담도담 2018. 4. 14. 11:16




이전글에서 listview_item.xml의 버튼이 나오지 않았다 ㅠ_ㅠ..

레이아웃의 높이 설정과 가중치 때문!

이전글 - [Android] - Custom List View 게시판 만들기



맨 처음 listview_item.xml 을 살펴보면 LinearLayout의 높이가 "match_parent"다.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="match_parent">


그러다보니 원래 listview의 높이만큼 아이템의 레이아웃이 축소되면서

버튼이 보이지 않았다.




다시 아이템의 LinearLayout의 높이를 "wrap_context"로 설정해보니

listview_item.xml 내에서 이전과 똑같은 현상이 일어난다.

각각의 뷰에 높이가 아니라 가중치를 주면서 wrap_context 또는 match_parent하여

이렇게 되는 듯 하다.


( 가중치로 하는 이유는 임의로 높이 도는 넓이를 설정했을 경우에

휴대폰마다 화면의 크기가 다르다보니 다른 폰으로 실행 했을 경우에

이상하게 나온 경험이 있다... ㅠ_ㅠ.. )



펜으로 그리면서 내가 필요한 레이아웃이 뭔가 보니

LinearLayout이 아니라 표모양의 레이아웃이 필요했다.


따라서 TableLayout으로 바꾼 뒤 가중치를 줬다 !

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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="wrap_content"
android:orientation="vertical"
android:weightSum="10">


<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />

<TextView
android:id="@+id/userID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:text="TextView"
android:textSize="22sp" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">

<ImageView
android:id="@+id/userIcon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:scaleType="centerInside"
app:srcCompat="@drawable/user" />

<TextView
android:id="@+id/context"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:text="context"
android:textSize="20sp" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">

<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="20sp" />

<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Button" />

<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Button" />
</TableRow>
</TableLayout>




짠 ! ㅎ3ㅎ