繁体   English   中英

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

[英]i want one (left or right side depending on user's choice)of the linearlayout to be replaced with a custom Linear layout

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

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);

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM