Создание общих представлений в android xml - PullRequest
0 голосов
/ 14 января 2020

У меня линейная раскладка с weightSum=3 и orientation=horizontal. В линейном макете у меня есть 3 дочерних линейных макета, каждый из которых имеет layout_weight=1, значок и текстовое представление, как показано ниже:

Проблема, с которой я сталкиваюсь, заключается в том, что мне не нужно редактировать каждый из линейные макеты в нескольких местах, есть ли способ заключить это? где я могу просто установить значок и текстовое представление и инкапсулированный отступ, масштабный тип изображения

enter image description here

Вот мой xml, я Показываю первые два для краткости:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3"
        android:layout_marginLeft="@dimen/paddingLarge"
        android:layout_marginRight="@dimen/paddingLarge"
        android:layout_marginTop="@dimen/paddingLarge">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="@dimen/paddingSmall">

            <ImageView
                android:layout_width="@dimen/bottomsheet_option_icon"
                android:layout_height="@dimen/bottomsheet_option_icon"
                android:src="@drawable/priority_icon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:text="Priority"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            android:padding="@dimen/paddingSmall"
            >

            <ImageView
                android:layout_width="@dimen/bottomsheet_option_icon"
                android:layout_height="@dimen/bottomsheet_option_icon"
                android:scaleType="centerCrop"
                android:src="@drawable/date_time_icon"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Priority"
                android:textColor="@android:color/white"/>


        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center">

            <ImageView
                android:layout_width="@dimen/bottomsheet_option_icon"
                android:layout_height="@dimen/bottomsheet_option_icon"
                android:scaleType="centerCrop"
                android:src="@drawable/notification_icon"/>

            <TextView
                android:textColor="@android:color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Priority"/>

        </LinearLayout>
    </LinearLayout>

1 Ответ

0 голосов
/ 14 января 2020

Создать «CustomView». Включите инфляцию в структуру «CustomLinearLayout», которая унаследовала «LinearLayout». Затем создайте контроллер для «ImageView» и «TextView».

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:padding="@dimen/paddingSmall">

    <ImageView
        android:layout_width="@dimen/bottomsheet_option_icon"
        android:layout_height="@dimen/bottomsheet_option_icon"
        android:src="@drawable/priority_icon" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="Priority"/>
</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...