簡體   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