java是否可以使用setWeightSum(float)以编程方式更改weightSum值?
我试图从片段控制线性布局的权重和。我想知道是否可以使用setWeightSum(float)方法来实现这一点。 我试图在id为header\u Linear的线性布局中控制权重和
<LinearLayout
安卓:id="@+id/header_Linear"
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="?安卓:attr/actionBarSize"
安卓:orientation="horizontal"
安卓:weightSum="8">
<TextView
安卓:id="@+id/table_0.0"
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="1"
安卓:visibility="visible"
安卓:background="@drawable/header_bg"/>
<TextView
安卓:id="@+id/table_0.1"
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="1"
安卓:textAlignment="center"
安卓:gravity="center"
安卓:text="M"
安卓:textColor="@color/colorTableItem"
安卓:visibility="visible"
安卓:background="@drawable/header_bg"/>
<TextView
安卓:id="@+id/table_0.2"
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="1"
安卓:gravity="center"
安卓:text="T"
安卓:textColor="@color/colorTableItem"
安卓:visibility="visible"
安卓:background="@drawable/header_bg"/>
<TextView
安卓:id="@+id/table_0.3"
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="1"
安卓:gravity="center"
安卓:text="W"
安卓:textColor="@color/colorTableItem"
安卓:visibility="visible"
安卓:background="@drawable/header_bg"/>
<TextView
安卓:id="@+id/table_0.4"
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="1"
安卓:gravity="center"
安卓:text="T"
安卓:visibility="visible"
安卓:textColor="@color/colorTableItem"
安卓:background="@drawable/header_bg"/>
<TextView
安卓:id="@+id/table_0.5"
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="1"
安卓:gravity="center"
安卓:text="F"
安卓:visibility="visible"
安卓:textColor="@color/colorTableItem"
安卓:background="@drawable/header_bg"/>
<TextView
安卓:id="@+id/table_0.6"
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="1"
安卓:gravity="center"
安卓:text="S"
安卓:textColor="@color/colorTableItem"
安卓:visibility="visible"
安卓:background="@drawable/header_bg"/>
<TextView
安卓:id="@+id/table_0.7"
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="1"
安卓:gravity="center"
安卓:text="S"
安卓:textColor="@color/sunday_text_color"
安卓:visibility="visible"
安卓: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而不出错。 多谢各位
Upate
The reason why I got nullPointException even though I declared Linear Layout using findViewById was due to
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
in Layout.The View that contains
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
would return null after using findViewById in this situation
# 1 楼答案