Можно ли программно изменить значение weightSum с помощью setWeightSum (float)? - PullRequest
0 голосов
/ 24 марта 2020

Я пытаюсь контролировать weightSum для линейного макета из фрагмента. Я задавался вопросом, возможно ли это сделать с помощью метода setWeightSum (float). Я пытался контролировать сумму веса в линейном макете, идентификатор которого равен header_Linear.

<LinearLayout
android:id="@+id/header_Linear"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:orientation="horizontal"
android:weightSum="8">

        <TextView
            android:id="@+id/table_0.0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textAlignment="center"
            android:gravity="center"
            android:text="M"
            android:textColor="@color/colorTableItem"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="T"
            android:textColor="@color/colorTableItem"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="W"
            android:textColor="@color/colorTableItem"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="T"
            android:visibility="visible"
            android:textColor="@color/colorTableItem"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="F"
            android:visibility="visible"
            android:textColor="@color/colorTableItem"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="S"
            android:textColor="@color/colorTableItem"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>

        <TextView
            android:id="@+id/table_0.7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="S"
            android:textColor="@color/sunday_text_color"
            android:visibility="visible"
            android:background="@drawable/header_bg"/>
</LinearLayout>

Это мой фрагмент, который использует setWeightSum

public class TimeTableFragment extends Fragment {

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_timetable, container, false);

    LinearLayout linearLayout = root.findViewById(R.id.header_Linear);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(getContext()));
    String header_value = sharedPreferences.getString("Header","0");
    Toast.makeText(getContext(),""+header_value,Toast.LENGTH_LONG).show();

    if (header_value.equals("0")){
        linearLayout.setWeightSum(6);
    }

    return root;
}

Когда я запускаю приложение это падает. Я хотел бы знать, возможно ли контролировать вес без ошибок. Большое спасибо

Upate

Причина, по которой я получил nullPointException, хотя я объявил Linear Layout с использованием findViewById, была вызвана xmlns:android="http://schemas.android.com/apk/res/android" в Layout.

Представление, содержащее xmlns:android="http://schemas.android.com/apk/res/android", вернет ноль после использования findViewById в этой ситуации

1 Ответ

0 голосов
/ 24 марта 2020
        public class TimeTableFragment extends Fragment {

        public View onCreateView(@NonNull LayoutInflater inflater,
                                 ViewGroup container, Bundle savedInstanceState) {

            View root = inflater.inflate(R.layout.fragment_timetable, container, false);

            return root;
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            LinearLayout linearLayout = view.findViewById(R.id.header_Linear);

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(getContext()));
            String header_value = sharedPreferences.getString("Header","0");
            Toast.makeText(getContext(),""+header_value,Toast.LENGTH_LONG).show();

            if (header_value.equals("0")){
                linearLayout.setWeightSum(6);
            }

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