java - 是否可以使用 setWeightSum(float) 以编程方式更改 weightSum 值?
问题描述
我正在尝试从片段控制 weightSum 的线性布局。我想知道是否可以使用 setWeightSum(float) 方法来做到这一点。我试图在 ID 为 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;
}
当我启动应用程序时它崩溃了。我想知道是否可以无错误地控制 weightSum。非常感谢
更新
即使我使用 findViewById 声明了线性布局,我得到 nullPointException 的原因是由于
xmlns:android="http://schemas.android.com/apk/res/android"
在布局中。
xmlns:android="http://schemas.android.com/apk/res/android"
在这种情况下使用 findViewById 后,包含的视图将返回 null
解决方案
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);
}
}
}
推荐阅读
- node.js - 简单的 Axios 函数导致 ECONNRESET,不确定如何调试
- c# - unity上按时读左键为右键
- c++ - SFML 中的 2D 水波纹效果不起作用,产生黑屏
- vue.js - PhpStorm 在 Vue.js 中的 :src 指令上显示警告
- vb.net - 如何获取选定的页码以仅打印 PrintRange.Selection 中的选定页面?
- python - 如何使用 pyinstaller 修复 ImportError?
- twilio - Twilio 可编程视频是否有“测试室”?
- javascript - 在构造函数中访问包含对象
- python - 如何仅对熊猫数据框中的选定列进行排序
- excel - 筛选后选择列