Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

그리드레이아웃 문제 #28

Open
settle54 opened this issue Oct 8, 2024 · 1 comment
Open

그리드레이아웃 문제 #28

settle54 opened this issue Oct 8, 2024 · 1 comment

Comments

@settle54
Copy link
Contributor

settle54 commented Oct 8, 2024

어떤 버그인가요?

그리드 레이아웃의 텍스트뷰들의 간격이 마음대로 입니다...
(6주차의 코드 내용으로 아직 커밋하지 않았습니다)

            <GridLayout
                android:id="@+id/categoryGrid"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:alignmentMode="alignMargins"
                android:background="@drawable/categories_background"
                android:columnCount="3"
                android:orientation="horizontal"
                android:padding="10dp">

                <TextView
                    android:id="@+id/noCategoryText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/dont_exist_catrgory" />
            </GridLayout>
private var categoryArray = arrayOf("힐링", "공부", "놀이", "zzzzzzz", "ddddd")

private fun setCategories() {
        if (categoryArray.isNotEmpty()) {
            binding.noCategoryText.visibility = View.GONE
            for (category in categoryArray) {
                val textView = TextView(context).apply {
                    text = resources.getString(R.string.category_text, category)
                    textSize = 16f
                    layoutParams = GridLayout.LayoutParams().apply {
                        width = GridLayout.LayoutParams.WRAP_CONTENT
                        height = GridLayout.LayoutParams.MATCH_PARENT

                        setGravity(Gravity.CENTER)
                    }
                    background =
                        resources.getDrawable(R.drawable.category_gray_background, null)
                    setPadding(15, 10, 15, 10)
                    setTextColor(ContextCompat.getColor(requireContext(), R.color.search_gray))

                    setOnClickListener {
                        val currentColor = currentTextColor
                        val searchGrayColor = ContextCompat.getColor(requireContext(), R.color.search_gray)

                        if (currentColor != searchGrayColor) {
                            setTextColor(ContextCompat.getColor(requireContext(), R.color.search_gray))
                            background = resources.getDrawable(R.drawable.category_gray_background, null)
                        } else {
                            setTextColor(ContextCompat.getColor(requireContext(), R.color.white))
                            background = resources.getDrawable(R.drawable.category_background, null)
                        }
                    }
                }
                binding.categoryGrid.addView(textView)
            }
        }
    }
// R.drawable.category_gray_background
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <corners android:radius="10dp" />
    <stroke
        android:width="2dp"
        android:color="#B6B6B6" />
</shape>

참고 자료

image
@LeeOhHyung
Copy link

아하, GridLayout에 추가되는 자식 View의 width를 임의로 하기 보다는 weight로 균등하게 만들어지는 방법이 있을것 같습니다.
찾아보니 layout_rowWeight ,layout_columnWeight 이라는 속성이 있는데요. 이것을 사용해보셨었나요? 개수가 고정적이지 않다면 RecyclerView + GridLayoutManager 를 사용하는것도 해결책 중에 하나가 될것 같긴합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants