Невозможно установить ширину горизонтальной линейной разметки или высоту вертикальной линейной разметки - PullRequest
0 голосов
/ 20 сентября 2018

Я сделал макет фрагмента, код которого показан ниже.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/bill_display_id"
    android:layout_gravity="start"
    tools:context=".HomePage">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/zoom">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bill_text_id"
            />

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/scroll_horiz_id">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:drawable/btn_default"
                android:orientation="horizontal"
                android:id="@+id/zoom_lin">

                <View
                    android:layout_height="match_parent"
                    android:layout_width="1dp"
                    android:background="@android:color/black" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="20dp"
                    android:id="@+id/prd_id">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Product"
                        android:layout_gravity="center"
                        />

                </LinearLayout>

                <View
                    android:layout_height="match_parent"
                    android:layout_width="1dp"
                    android:background="@android:color/black" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="20dp"
                    android:id="@+id/rate_id">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Rate"
                        android:layout_gravity="center"
                        />

                </LinearLayout>

                <View
                    android:layout_height="match_parent"
                    android:layout_width="1dp"
                    android:background="@android:color/black" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="20dp"
                    android:id="@+id/qty_id">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Qty"
                        android:layout_gravity="center"
                        />

                </LinearLayout>

                <View
                    android:layout_height="match_parent"
                    android:layout_width="1dp"
                    android:background="@android:color/black" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="20dp"
                    android:id="@+id/price_id">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Price"
                        android:layout_gravity="center"
                        />

                </LinearLayout>

                <View
                    android:layout_height="match_parent"
                    android:layout_width="1dp"
                    android:background="@android:color/black" />

            </LinearLayout>

        </HorizontalScrollView>

    </LinearLayout>

</ScrollView>

Теперь у меня есть функция setZoom (показанная ниже), в которой я пытаюсь установить высоту иширина горизонтальной линейной разметки (id = zoom_lin).Но я вижу, что устанавливается только высота, а не ширина.

protected void setZoom(float val)
{

    View horz_lin = rootView.findViewById(R.id.zoom_lin);
    ViewGroup.LayoutParams lp = horz_lin.getLayoutParams();
    lp.width = 300;    // 300 is a random number for testing only
    lp.height = 600;   // 600 is a random number for testing only
    horz_lin.requestLayout();

}

Точно так же, если я пытаюсь установить высоту и ширину для вертикального линейного расположения (id = zoom), я вижу, что устанавливается только ширинаа не высота.

Существует ли какое-либо правило, которое запрещает нам устанавливать ширину для горизонтального линейного расположения и высоту для вертикального линейного расположения.

Пожалуйста, извините, если я упускаю что-то тривиальное, так как яновичок в разработке Android.

1 Ответ

0 голосов
/ 20 сентября 2018

Необходимо установить обновленный layoutParams обратно в линейный макет:

protected void setZoom(float val)
{

    LinearLayout horz_lin = (LinearLayout)rootView.findViewById(R.id.zoom_lin);
    ViewGroup.LayoutParams lp = horz_lin.getLayoutParams();
    lp.width = 300;    // 300 is a random number for testing only
    lp.height = 600;   // 600 is a random number for testing only
    horz_lin.setLayoutParams(lp); // ADD THIS LINE
    horz_lin.requestLayout();

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...