У меня есть основной макет, который включает в себя мой вид, в котором есть RecyclerView и на него нанесена сетка. Теперь элементы имеют ширину по умолчанию из-за возможности рисования, которую я применяю, и теперь они выровнены по левому краю, а не центрируют то, что я хочу достичь.
Мой основной макет:
<android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "0dp"
android:layout_height = "0dp"
android:id = "@+id/main_view"
xmlns:app = "http://schemas.android.com/apk/res-auto">
<include android:id = "@+id/constraint_more"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_gravity = "center_horizontal"
android:visibility = "gone"
layout = "@layout/list_more_view"/>
</android.support.constraint.ConstraintLayout>
Больше макета:
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
android:id = "@+id/list_more_view"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "@drawable/list_more_bg">
<TextView
android:id = "@+id/textview_title_more"
android:layout_width = "wrap_content"
android:layout_height = "50dp"
android:gravity = "center_vertical"
android:paddingLeft = "20dp"
android:shadowRadius = "2"
android:text = "@string/title"
android:textColor = "@color/list_text_color"
android:textSize = "25dp"/>
<ImageView
android:id = "@+id/button_closemore"
android:layout_width = "40dp"
android:layout_height = "40dp"
android:layout_marginEnd = "10dp"
android:layout_marginRight = "10dp"
android:contentDescription = "@string/close_button"
android:src = "@drawable/btn_close"
app:layout_constraintBottom_toBottomOf = "@id/textview_title_more"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintTop_toTopOf = "parent" />
<android.support.v7.widget.RecyclerView
android:id = "@+id/recyclerview_morelist"
android:layout_width = "match_parent"
android:layout_height = "0dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintTop_toBottomOf = "@id/textview_title_more" />
</android.support.constraint.ConstraintLayout>
И раскладка товара:
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
android:id = "@+id/constraintlayout_item_view"
android:layout_width = "150dp"
android:layout_height = "wrap_content"
android:background = "@drawable/plate_basic">
<TextView
android:id = "@+id/textview_unit_title"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:maxLines = "2"
app:autoSizeTextType = "uniform"
android:textColor = "@color/item_text_color"
android:paddingTop = "@dimen/tile_title_padding"
app:layout_constraintTop_toTopOf = "parent"/>
</android.support.constraint.ConstraintLayout>
Таким образом, все элементы в этом дополнительном представлении выровнены по левому краю, но я хочу, чтобы все элементы были центрированы, я попытался установить ширину для обертывания содержимого, а затем центрирование с помощью гравитации, но безуспешно.
Измените constraintlayout_item_view, чтобы иметь:
android:layout_width = "match_parent"
Затем установите textview_unit_title, чтобы иметь:
android:layout_width = "wrap_content"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
Это должно разместить TextView по центру внутри его строки.
Вдобавок ширина и высота вашего основного макета выглядят немного странно:
android:layout_width = "0dp"
android:layout_height = "0dp"
Вероятно, они оба должны быть match_parent
Это отлично работает для gird, но, как я уже сказал, я использую этот элемент в другом представлении ресайклера, где есть горизонтальная прокрутка и отображается несколько элементов, если я использую родительский элемент соответствия, то в горизонтальном ресайклере будет виден только 1 элемент на каждое движение
Нет, ты никогда не говорил.
есть ли причина использовать 150dp в качестве ширины в макете вашего элемента?