我希望将线性布局中的一个(左侧或右侧取决于用户的选择)替换为自定义线性布局



我在安卓应用程序开发方面很新手,在制作应用程序时遇到了问题。我正在制作一个与板球相关的应用程序。我希望将线性布局的一个(左侧或右侧取决于用户的选择(替换为自定义线性布局。这是我的主活动代码

package com.pratham.cricket1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public TextView t1,t2;
public int ta,tb;
public int w1,w2;
public int balls,w,t;
LinearLayout l1,l2;
RelativeLayout r1;
ViewGroup vg1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i2=getIntent();
Bundle b2=i2.getExtras();
if(!b2.isEmpty()){
balls=b2.getInt("Balls");
t=b2.getInt("Team won toss");
w=b2.getInt("Winning team chose");
}else{}
t1=findViewById(R.id.TeamA);
ta=0;
t2=findViewById(R.id.TeamB);
ta=0;
w1=0;
w2=0;
l1=(LinearLayout)findViewById(R.id.a);
l2=(LinearLayout)findViewById(R.id.b);
vg1=(ViewGroup)l2.getParent();
r1=(RelativeLayout)findViewById(R.id.bowling);
TeamABatting();

}
public void a1(View view){
score_add(1,t1);
}
public void a2(View view){
score_add(2,t1);
}
public void a3(View view){
score_add(3,t1);
}
public void a4(View view){
score_add(4,t1);
}
public void a6(View view){
score_add(6,t1);
}
public void b1(View view){
score_add(1,t2);
}
public void b2(View view){
score_add(2,t2);
}
public void b3(View view){
score_add(3,t2);
}
public void b4(View view){
score_add(4,t2);
}
public void b6(View view){
score_add(6,t2);
}
public  void score_add(int a ,TextView t3){
if(t3==findViewById(R.id.TeamA)){
ta+=a;
Score_doner(t3,ta,w1);
}
if(t3==findViewById(R.id.TeamB)){
tb+=a;
Score_doner(t3,tb,w2);
}

}
public void TeamAOut(View view){
w1++;
Score_doner(t1,ta,w1);
}
public void TeamBOut(View view){
w2++;
Score_doner(t2,tb,w2);
}
public void Score_doner(TextView t4,int runs,int wicket){
t4.setText(Integer.toString(runs)+"/"+Integer.toString(wicket));
}
public void TeamABatting(){
vg1.removeView(l2);
View v1=getLayoutInflater().inflate(R.layout.bowling,null);
vg1.addView(v1);


}
public void TeamBBatting(){
vg1.removeView(l1);
View v1=getLayoutInflater().inflate(R.layout.bowling,null);
vg1.addView(v1);
}
}

这是我的主要.xml

?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:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="horizontal"
tools:context="com.pratham.cricket1.MainActivity">
<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>
<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
android:textSize="96dp"
style="@style/common_text"
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f20404"
android:text="OUT!!"
android:textColor="@android:color/black"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

我希望它恰好分成屏幕的一半。但我没有达到最终结果。忽略过多的编码行,我稍后会干燥它们。这是我希望它被替换的线性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:background="@drawable/border"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/bowling"
android:layout_width="match_parent"

android:layout_height="match_parent"
>
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="Balls Left:"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/ballsleft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_toRightOf="@id/hello" />
<ListView
android:id="@+id/overs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ballsleft">
</ListView>

</RelativeLayout>
</LinearLayout>

我希望根据用户的选择动态添加它。布局应仅覆盖屏幕的一半 在此处输入图像描述 如下图所示。 但我想要它正好是屏幕的一半

我认为问题出在您命名为 v1 和 v2 的 viewGroup 上。我尝试的是指导从 l1 和 l2 中删除视图,并分别通过线性布局中的膨胀添加新视图。 下面的代码是TeamA和TeamB的两种方法的一瞥。

public void TeamABatting(){
l2.removeAllViews();
View v1=getLayoutInflater().inflate(R.layout.demo,null);
l2.addView(v1);


}
public void TeamBBatting(){
l1.removeAllViews();
View v1=getLayoutInflater().inflate(R.layout.demo,null);
l1.addView(v1);
}

希望有帮助。

编辑部分。 这是活动的完整代码。

public class Main2Activity extends AppCompatActivity {
public TextView t1,t2;
public int ta,tb;
public int w1,w2;
public int balls = 10,w =1,t =1;
LinearLayout l1,l2;
RelativeLayout r1;
ViewGroup vg1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
t1=findViewById(R.id.TeamA);
ta=0;
t2=findViewById(R.id.TeamB);
ta=0;
w1=0;
w2=0;
l1=(LinearLayout)findViewById(R.id.a);
l2=(LinearLayout)findViewById(R.id.b);
vg1=(ViewGroup)l2.getParent();
r1=(RelativeLayout)findViewById(R.id.bowling);
TeamABatting();

}
public void a1(View view){
score_add(1,t1);
}
public void a2(View view){
score_add(2,t1);
}
public void a3(View view){
score_add(3,t1);
}
public void a4(View view){
score_add(4,t1);
}
public void a6(View view){
score_add(6,t1);
}
public void b1(View view){
score_add(1,t2);
}
public void b2(View view){
score_add(2,t2);
}
public void b3(View view){
score_add(3,t2);
}
public void b4(View view){
score_add(4,t2);
}
public void b6(View view){
score_add(6,t2);
}
public  void score_add(int a ,TextView t3){
if(t3==findViewById(R.id.TeamA)){
ta+=a;
Score_doner(t3,ta,w1);
}
if(t3==findViewById(R.id.TeamB)){
tb+=a;
Score_doner(t3,tb,w2);
}

}
public void TeamAOut(View view){
w1++;
Score_doner(t1,ta,w1);
}
public void TeamBOut(View view){
w2++;
Score_doner(t2,tb,w2);
}
public void Score_doner(TextView t4,int runs,int wicket){
t4.setText(Integer.toString(runs)+"/"+Integer.toString(wicket));
}
public void TeamABatting(){
l2.removeAllViews();
View v1=getLayoutInflater().inflate(R.layout.demo,null);
l2.addView(v1);


}
public void TeamBBatting(){
l1.removeAllViews();
View v1=getLayoutInflater().inflate(R.layout.demo,null);
l1.addView(v1);
}
}

最新更新