Android开发:如何根据用户选择替换LinearLayout单侧为自定义布局
Hey there! Let's work through your cricket app layout replacement issue step by step. I've looked over your code and spotted a few areas we can tweak to get this working smoothly.
1. Bundle Null Safety
First off, your current code checks if(!b2.isEmpty()) but doesn't first verify if b2 is null. This can cause a NullPointerException if no extras are passed. Let's fix that:
Intent i2 = getIntent(); Bundle b2 = i2.getExtras(); if (b2 != null) { // Check for null first to avoid crashes! balls = b2.getInt("Balls"); t = b2.getInt("Team won toss"); w = b2.getInt("Winning team chose"); }
2. Layout Replacement Logic
Your TeamABatting() and TeamBBatting() methods have two small issues:
- You're not attaching the inflated view to the parent container properly (which can break layout weights)
- Relying on
l2.getParent()to get the main container is fragile – better to reference it directly with an ID
First, add an ID to your root LinearLayout in activity_main.xml:
<LinearLayout android:id="@+id/main_container" <!-- Add this line --> android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="2" android:orientation="horizontal" ...>
Then update your layout replacement methods and initialize the main container in onCreate():
// Add this class-level variable LinearLayout mainContainer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... // Initialize the main container mainContainer = findViewById(R.id.main_container); ... } public void TeamABatting() { // Replace Team B's section with the bowling layout if (mainContainer != null && l2 != null) { mainContainer.removeView(l2); // Inflate with main container as parent to preserve layout weights View bowlingView = getLayoutInflater().inflate(R.layout.bowling, mainContainer, false); mainContainer.addView(bowlingView); } } public void TeamBBatting() { // Replace Team A's section with the bowling layout if (mainContainer != null && l1 != null) { mainContainer.removeView(l1); View bowlingView = getLayoutInflater().inflate(R.layout.bowling, mainContainer, false); mainContainer.addView(bowlingView); } }
3. Minor Code Cleanups
- You initialized
tatwice inonCreate()– remove one instance, and add initialization fortb:ta = 0; tb = 0; // Add this line to avoid uninitialized variable issues - Optimize your
score_add()method to avoid redundantfindViewById()calls (you already have references tot1andt2):public void score_add(int a, TextView t3) { if (t3 == t1) { ta += a; Score_doner(t1, ta, w1); } else if (t3 == t2) { tb += a; Score_doner(t2, tb, w2); } }
4. Complete Your Layout XML
Your activity_main.xml cut off mid-way for Team B's section. Here's the full completed version to match Team A's structure:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/main_container" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="2" android:orientation="horizontal" tools:context="com.pratham.cricket1.MainActivity"> <!-- Team A Section --> <LinearLayout android:id="@+id/a" android:background="@drawable/border" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <TextView style="@style/common_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Team A" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView style="@style/common_text" android:textSize="96dp" android:text="0/0" android:id="@+id/TeamA" android:layout_width="match_parent" android:layout_height="160dp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="a1" android:text="+1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="a2" android:text="+2" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="a3" android:text="+3" /> <Button android:layout_width="match_parent" android:layout_height="84dp" android:onClick="a4" android:text="+4" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:onClick="a6" android:text="+6" /> <Button android:onClick="TeamAOut" android:textColor="@android:color/black" android:textStyle="bold" android:layout_width="match_parent" android:layout_height="match_parent" android:text="OUT!!" android:background="#f70303" /> </LinearLayout> <!-- Team B Section --> <LinearLayout android:id="@+id/b" android:background="@drawable/border" android:orientation="vertical" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:text="Team B" style="@style/common_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView style="@style/common_text" android:textSize="96dp" android:text="0/0" android:id="@+id/TeamB" android:layout_width="match_parent" android:layout_height="160dp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="b1" android:text="+1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="b2" android:text="+2" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="b3" android:text="+3" /> <Button android:layout_width="match_parent" android:layout_height="84dp" android:onClick="b4" android:text="+4" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:onClick="b6" android:text="+6" /> <Button android:onClick="TeamBOut" android:textColor="@android:color/black" android:textStyle="bold" android:layout_width="match_parent" android:layout_height="match_parent" android:text="OUT!!" android:background="#f70303" /> </LinearLayout> </LinearLayout>
With these changes, your layout replacement should work as expected:
- Call
TeamABatting()when you want to replace the right side (Team B) with your bowling layout - Call
TeamBBatting()when you want to replace the left side (Team A) with your bowling layout
Make sure your bowling.xml layout uses match_parent for width (since it's taking the place of a 0dp width, weight=1 child) to fit correctly in the main container.
内容的提问来源于stack exchange,提问作者pratham powar

