Создать ImageButton после нажатия на ImageButton - PullRequest
0 голосов
/ 05 апреля 2020

У меня есть два ряда ImageButton с. После нажатия на любой из первого ряда я хочу удалить первый ряд, а второй - с центром в ImageButton. Это должно выглядеть примерно так: «https://lifehacker.ru/special/travel-checklist/».

Макет первого ряда:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#bbbbbb"
    android:orientation="horizontal">


    <ImageButton
        android:id="@+id/winter"
        android:layout_width="192dp"
        android:layout_height="183dp"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:gravity="left|center_vertical"
        android:onClick="Winter"
        android:paddingTop="32sp"
        android:scaleType="fitCenter"
        android:src="@drawable/snejinka"
        android:text="Зима"
        android:textColor="#FFFFFF" />


    <ImageButton
        android:id="@+id/Middle"
        android:layout_width="192dp"
        android:layout_height="183dp"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:gravity="left|center_vertical"
        android:onClick="Middle"
        android:paddingTop="32sp"
        android:scaleType="fitCenter"
        android:src="@drawable/listok"
        android:text="Весна/Осень"
        android:textColor="#FFFFFF" />

    <ImageButton
        android:id="@+id/Summer"
        android:layout_width="192dp"
        android:layout_height="183dp"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:gravity="left|center_vertical"
        android:onClick="Summer"
        android:paddingTop="32sp"
        android:scaleType="fitCenter"
        android:src="@drawable/unnamed"
        android:text="Лето"
        android:textColor="#FFFFFF" />


</LinearLayout>

Макет второго ряда:

<ImageButton
            android:id="@+id/three"
            android:layout_width="192dp"
            android:layout_height="183dp"
            android:layout_alignParentRight="true"
            android:layout_weight="2"
            android:gravity="left|center_vertical"
            android:onClick="three"
            android:paddingTop="32sp"
            android:scaleType="fitCenter"
            android:src="@drawable/three"
            android:text="three"
            android:textColor="#FFFFFF" />
        <ImageButton
            android:id="@+id/seven"
            android:layout_width="192dp"
            android:layout_height="183dp"
            android:layout_alignParentRight="true"
            android:layout_weight="2"
            android:gravity="left|center_vertical"
            android:onClick="seven"
            android:paddingTop="32sp"
            android:scaleType="fitCenter"
            android:src="@drawable/seven"
            android:text="three"
            android:textColor="#FFFFFF" />
        <ImageButton
            android:id="@+id/fourteen"
            android:layout_width="192dp"
            android:layout_height="183dp"
            android:layout_alignParentRight="true"
            android:layout_weight="2"
            android:gravity="left|center_vertical"
            android:onClick="fourteen"
            android:paddingTop="32sp"
            android:scaleType="fitCenter"
            android:src="@drawable/fourteen"
            android:text="three"
            android:textColor="#FFFFFF" />

А вот и моя активность для обеих строк. Я не знаю, как заставить второй ряд появляться после первого клика (второй ряд под комментариями)

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import static com.example.samsunghomework.R.id.*;

public class MainActivity extends AppCompatActivity {
    ImageButton winter;
    ImageButton middle;
    ImageButton summer;
    LinearLayout mainlayout;

    /*
     ImageButton three;
     ImageButton seven;
     ImageButton fourteen;
 */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        winter = (ImageButton) findViewById(R.id.winter);
        middle = (ImageButton) findViewById(Middle);
        summer = (ImageButton) findViewById(Summer);
        mainlayout = (LinearLayout) findViewById(R.id.mainlayout);
        /*
        three = (ImageButton) findViewById(R.id.three);
        seven = (ImageButton) findViewById(R.id.seven);
        fourteen = (ImageButton) findViewById(R.id.fourteen);
     */
    }


    int galka = 0;
    int curr = 0;

    public void Winter(View view) {
        //   mainlayout.removeAllViews();
        if (galka == 0) winter.setImageResource(R.drawable.galka);

        else if (curr == 2) {
            middle.setImageResource(R.drawable.listok);
            winter.setImageResource(R.drawable.galka);
        }
        if (curr == 3) {
            summer.setImageResource(R.drawable.unnamed);
            winter.setImageResource(R.drawable.galka);
        }
        galka = 1;
        curr = 1;
      //  mainlayout.addView(new ImageButton(this));

        ImageButton imageButton = new ImageButton(MainActivity.this);
        imageButton.setImageResource(R.drawable.galka);
        LinearLayout.LayoutParams imageViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        imageButton.setLayoutParams(imageViewLayoutParams);

        mainlayout.addView(imageButton);
    }


    public void Middle(View view) {
        if (galka == 0) middle.setImageResource(R.drawable.galka);
        else if (curr == 1) {
            winter.setImageResource(R.drawable.snejinka);
            middle.setImageResource(R.drawable.galka);
        }
        if (curr == 3) {
            summer.setImageResource(R.drawable.unnamed);
            middle.setImageResource(R.drawable.galka);
        }
        galka = 1;
        curr = 2;
    }

    public void Summer(View view) {
        if (galka == 0) summer.setImageResource(R.drawable.galka);
        else if (curr == 1) {
            winter.setImageResource(R.drawable.snejinka);
            summer.setImageResource(R.drawable.galka);
        }

        if (curr == 2) {
            middle.setImageResource(R.drawable.listok);
            summer.setImageResource(R.drawable.galka);

        }
        galka = 1;
        curr = 3;
    }

    int galka2 = 0;
    int curr2 = 0;
/*
    public void three(View view) {
        if (galka2 == 0) three.setImageResource(R.drawable.galka);
        else if (curr2 == 2) {
            seven.setImageResource(R.drawable.seven);
            three.setImageResource(R.drawable.galka);
        }
        if (curr2 == 3) {
            fourteen.setImageResource(R.drawable.fourteen);
            three.setImageResource(R.drawable.galka);
        }
        galka2 = 1;
        curr2 = 1;

    }

    public void seven(View view) {
        if (galka2 == 0) seven.setImageResource(R.drawable.galka);
        else if (curr2 == 1) {
            three.setImageResource(R.drawable.three);
            seven.setImageResource(R.drawable.galka);
        }
        if (curr2 == 3) {
            fourteen.setImageResource(R.drawable.fourteen);
            seven.setImageResource(R.drawable.galka);
        }
        galka2 = 1;
        curr2 = 2;

    }

    public void fourteen(View view) {
        if (galka2 == 0) fourteen.setImageResource(R.drawable.galka);
        else if (curr2 == 1) {
            three.setImageResource(R.drawable.three);
            fourteen.setImageResource(R.drawable.galka);
        }

        if (curr2 == 2) {
            seven.setImageResource(R.drawable.seven);
            fourteen.setImageResource(R.drawable.galka);

        }
        galka2 = 1;
        curr2 = 3;

    }
*/
}

Ответы [ 2 ]

0 голосов
/ 05 апреля 2020

Вы можете использовать линейный макет и макет кадра, чтобы разделить каждую из них и показать кнопки изображений на основе события щелчка.

Макет. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#bbbbbb"
    android:orientation="horizontal">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="horizontal">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2">

            <ImageButton
                android:id="@+id/winter"
                android:layout_width="192dp"
                android:layout_height="183dp"
                android:layout_alignParentRight="true"
                android:layout_gravity="center"
                android:layout_weight="2"
                android:gravity="left|center_vertical"
                android:onClick="Winter"
                android:paddingTop="32sp"
                android:scaleType="fitCenter"
                android:src="@drawable/snejinka"
                android:text="Зима"
                android:textColor="#FFFFFF"
                android:visibility="visible" />

            <ImageButton
                android:id="@+id/seven"
                android:layout_width="192dp"
                android:layout_height="183dp"
                android:layout_alignParentRight="true"
                android:layout_gravity="center"
                android:layout_weight="2"
                android:gravity="left|center_vertical"
                android:onClick="seven"
                android:paddingTop="32sp"
                android:scaleType="fitCenter"
                android:src="@drawable/seven"
                android:text="three"
                android:textColor="#FFFFFF"
                android:visibility="invisible" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2">

            <ImageButton
                android:id="@+id/Middle"
                android:layout_width="192dp"
                android:layout_height="183dp"
                android:layout_alignParentRight="true"
                android:layout_gravity="center"
                android:layout_weight="2"
                android:gravity="left|center_vertical"
                android:onClick="Middle"
                android:paddingTop="32sp"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_launcher_background"
                android:text="Весна/Осень"
                android:textColor="#FFFFFF" />

            <ImageButton
                android:id="@+id/three"
                android:layout_width="192dp"
                android:layout_height="183dp"
                android:layout_alignParentRight="true"
                android:layout_gravity="center"
                android:layout_weight="2"
                android:gravity="left|center_vertical"
                android:onClick="three"
                android:paddingTop="32sp"
                android:scaleType="fitCenter"
                android:src="@drawable/three "
                android:text="three"
                android:textColor="#FFFFFF"
                android:visibility="invisible" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2">

            <ImageButton
                android:id="@+id/Summer"
                android:layout_width="192dp"
                android:layout_height="183dp"
                android:layout_alignParentRight="true"
                android:layout_gravity="center"
                android:layout_weight="2"
                android:gravity="left|center_vertical"
                android:onClick="Summer"
                android:paddingTop="32sp"
                android:scaleType="fitCenter"
                android:src="@mipmap/unnamed"
                android:text="Лето"
                android:textColor="#FFFFFF" />

            <ImageButton
                android:id="@+id/fourteen"
                android:layout_width="192dp"
                android:layout_height="183dp"
                android:layout_alignParentRight="true"
                android:layout_gravity="center"
                android:layout_weight="2"
                android:gravity="left|center_vertical"
                android:onClick="fourteen"
                android:paddingTop="32sp"
                android:scaleType="fitCenter"
                android:src="@drawable/fourteen"
                android:text="three"
                android:textColor="#FFFFFF"
                android:visibility="invisible" />
        </FrameLayout>

    </LinearLayout>


</LinearLayout>

MainActivity. java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.ImageButton;
        import android.widget.ImageView;
        import android.widget.LinearLayout;


public class MainActivity extends AppCompatActivity {
    LinearLayout mainlayout;
    ImageButton winter;
    ImageButton middle;
    ImageButton summer;
    ImageButton three;
    ImageButton seven;
    ImageButton fourteen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainlayout = findViewById(R.id.mainlayout);

        winter = findViewById(R.id.winter);
        middle = findViewById(R.id.Middle);
        summer = findViewById(R.id.Summer);

        three = findViewById(R.id.three);
        seven = findViewById(R.id.seven);
        fourteen = findViewById(R.id.fourteen);

        winter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                action();
            }
        });

        middle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                action();
            }
        });

        summer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                action();
            }
        });
    }

    public void action() {
        winter.setVisibility(View.INVISIBLE);
        middle.setVisibility(View.INVISIBLE);
        summer.setVisibility(View.INVISIBLE);

        three.setVisibility(View.VISIBLE);
        fourteen.setVisibility(View.VISIBLE);
        seven.setVisibility(View.VISIBLE);
    }
}
0 голосов
/ 05 апреля 2020

Вы можете воспроизвести видимость в линейном макете, в котором есть несколько кнопок ImageButton после того, как вы щелкнете по нескольким элементам ImageButton в первой строке.

Вы можете попробовать этот код.

activity_main. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/layoutRowOne"
        android:orientation="horizontal"
        android:background="@android:color/holo_blue_light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:text="YES"
            android:id="@+id/btnLayoutOneYes"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <Button
            android:text="NO"
            android:id="@+id/btnLayoutOneNo"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutRowTwo"
        android:orientation="horizontal"
        android:visibility="gone"
        android:background="@android:color/holo_orange_light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:text="HOT"
            android:id="@+id/btnLayoutTwoHot"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <Button
            android:text="COLD"
            android:id="@+id/btnLayoutTwoCold"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

</LinearLayout>

MainActivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private var statusRowOne = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnLayoutOneYes.setOnClickListener {
            statusRowOne = true
            if (statusRowOne) {
                layoutRowOne.visibility = View.GONE
                layoutRowTwo.visibility = View.VISIBLE
            }
        }
        btnLayoutOneNo.setOnClickListener {
            statusRowOne = true
            if (statusRowOne) {
                layoutRowOne.visibility = View.GONE
                layoutRowTwo.visibility = View.VISIBLE
            }
        }
    }
}

Надеюсь, этот код поможет вашему делу.

...