简体   繁体   English

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

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

I am very new in android app development and had a problem in making a app. 我是android应用程序开发的新手,制作应用程序时遇到问题。 I am making a app related to cricket. 我正在制作与板球相关的应用。 I want one (left or right side depending on user's choice) of the linearlayout to be replaced with a custom Linear layout. 我希望将线性布局的一个(左侧或右侧,取决于用户的选择)替换为自定义的线性布局。 This is my code for MainActivity 这是我的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);

    }
}

This is my main .xml 这是我的主要.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>

I want it to be divided in exact half the screen. 我希望将其分成屏幕的一半。 But I am not achieving the end result.Ignore the excessive coding lines, I will DRY them later. 但是我没有达到最终的结果,请忽略多余的编码行,稍后再将其干燥。 This is the linearlayout I want it to be replaced it with 这是线性布局,我希望将其替换为

<?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>

I want it to be dynamically added based on user's choice.The layouts should cover only half of the screen enter image description here It is like the image below. 我希望它可以根据用户的选择动态添加。布局应仅覆盖屏幕的一半,在此处输入图像说明 ,就像下面的图像一样。 But i want it exactly half.of the screen 但是我希望它恰好是屏幕的一半

I think the problem is with the viewGroup that you are taking named as v1 and v2. 我认为问题在于您正在使用的名为v1和v2的viewGroup。 What I have tried is directing removing the view from l1 and l2 and adding a new view through inflater in the linear layout respectively. 我试过的是直接从l1和l2中删除视图,并通过充气机分别在线性布局中添加新视图。 Code below is a glimpse the two methods of TeamA and TeamB. 下面的代码简要介绍了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);

    }

Hope that helps. 希望能有所帮助。

Edited Part. 编辑部分。 here is the full code of the activity. 这是活动的完整代码。

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.

相关问题 我想将位图左右边缘模糊掉 - I want bitmap Left and right side edges to be blurred out 根据用户属性以编程方式更改线性布局中的textview - Programmatically changing textview in Linear Layout depending on user attributes 在LinearLayout中将左侧图像移到右侧 - bring the left image to the right in LinearLayout LinearLayout从右到左填充 - LinearLayout filled from Right to Left 选择要执行的代码部分,具体取决于用户的选择 - Choose which part of code will be executed, depending on the user's choice 为什么将layout_gravity设置为left / right在水平LinearLayout中不起作用? - Why does layout_gravity set to left/right have no effect in horizontal LinearLayout? 如何将一个元素与 JPanel 的右侧对齐,而将另一个元素对齐到 JPanel 的左侧? - How can I align one element to the right and another to the left side of my JPanel? 如何在Java代码的帮助下将此聊天布局(黑色)移到右侧? - How do I move this chat layout(black one) to right side with the help of java code? 如何在LinearLayout上显示其他布局上的TextView? - How do I show TextView's in LinearLayout that layed on other Layout? 如何在单击按钮时将自定义布局添加到线性布局 - how do I add a custom layout to a Linear Layout on the click of a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM