单击按钮时如何添加不同的图层

时间:2014-10-13 11:58:51

标签: android layer

我有4张imageButton, 当我点击其中一个按钮时,一个图层应该出现在我创建的imageView上。

public void onClick(View v) {

    switch(v.getId()){

    case R.id.categoriaA1:

        // add layer a1
        break;

    case R.id.categoriaA2:
        // add layer a2
        break;
    case R.id.categoriaA3:
        //add  layer A3
        break;
    case R.id.categoriaA4:
        // add layer A4
        break;

myAvatar是我的ImageView。 有人能帮助我吗?

编辑这是我的xml代码(我有4个类别的20个imageButton)

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.settingavatar.MyAvatar" >

<!-- problemi con il dimensionamento -->

<ImageView
    android:id="@+id/anteprimaAvatar"
    android:layout_width="282dp"
    android:layout_height="150dp" 

    android:background="@drawable/btn_background"/>
<!-- griglia che contiene le 4 categorie per i vari oggetti che si possono aggiungere all'avatar -->

<GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/anteprimaAvatar"
    android:orientation="vertical" >

    <!-- sottogriglia per la categoria A -->

    <GridLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/categoriaA1"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/cata1"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaA2"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/cata2"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaA3"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/cata3"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaA4"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/cata4"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaA5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:tag="accessorio" />
    </GridLayout>

    <!-- sottogriglia per la categoria B -->

    <GridLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/categoriaB1"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catb1"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaB2"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catb2"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaB3"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catb3"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaB4"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catb4"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaB5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:tag="accessorio" />
    </GridLayout>

    <!-- sottogriglia per la categoria C -->

    <GridLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/categoriaC1"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catc1"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaC2"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catc2"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaC3"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catc3"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaC4"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catc4"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaC5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:tag="accessorio" />
    </GridLayout>

    <!-- sottogriglia per la categoria D -->

    <GridLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/categoriaD1"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catd1"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaD2"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catd2"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaD3"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catd3"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaD4"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:background="@drawable/catd4"
            android:tag="accessorio" />

        <ImageButton
            android:id="@+id/categoriaD5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:tag="accessorio" />
    </GridLayout>
</GridLayout>

3 个答案:

答案 0 :(得分:4)

对布局使用可见性操作

例如

        LinearLayout lly1 = (LinearLayout)findViewById(R.id.lly1);
        LinearLayout lly2 = (LinearLayout)findViewById(R.id.lly1);
        LinearLayout lly3 = (LinearLayout)findViewById(R.id.lly1);
        LinearLayout lly4 = (LinearLayout)findViewById(R.id.lly1);


  public void onClick(View v) {

switch(v.getId()){

case R.id.categoriaA1:

    // add layer a1
   lly1.setVisibility(v.VISIBLE);
   lly2.setVisibility(v.GONE);
   lly3.setVisibility(v.GONE);
   lly4.setVisibility(v.GONE);
    break;

case R.id.categoriaA2:
    // add layer a2
   lly2.setVisibility(v.VISIBLE);
   lly1.setVisibility(v.GONE);
   lly3.setVisibility(v.GONE);
   lly4.setVisibility(v.GONE);
    break;
case R.id.categoriaA3:
    //add  layer A3
   lly3.setVisibility(v.VISIBLE);
   lly2.setVisibility(v.GONE);
   lly1.setVisibility(v.GONE);
   lly4.setVisibility(v.GONE);
    break;
case R.id.categoriaA4:
    // add layer A4
   lly4.setVisibility(v.VISIBLE);
   lly2.setVisibility(v.GONE);
   lly3.setVisibility(v.GONE);
   lly1.setVisibility(v.GONE);

    break;

}

来布局activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/lly1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="visible" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lly2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lly3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lly4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >
    </LinearLayout>

</RelativeLayout>

答案 1 :(得分:0)

单击时,您可以更改目标imageView的源或背景。
例如

ImageView imageview = (ImageView) root.findViewById(R.id.categoriaA1);
imageview.setBackground(Drawable your_background);

答案 2 :(得分:0)

  <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:columnCount="1"
  android:orientation="vertical"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.settingavatar.MyAvatar" >



  <ImageView
android:id="@+id/anteprimaAvatar"
android:layout_width="282dp"
android:layout_height="150dp" 
android:background="@drawable/btn_background"/>

<LinearLayout 
 android:id="@+id/anteprimaAvatarLinear"
 android:layout_width="282dp"
 android:visibility="gone"
 android:layout_height="150dp"></LinearLayout>

<GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/anteprimaAvatar"
android:orientation="vertical" >

<!-- sottogriglia per la categoria A -->

<GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/categoriaA1"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/cata1"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaA2"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/cata2"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaA3"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/cata3"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaA4"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/cata4"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaA5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:tag="accessorio" />
</GridLayout>

<!-- sottogriglia per la categoria B -->

<GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/categoriaB1"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catb1"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaB2"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catb2"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaB3"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catb3"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaB4"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catb4"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaB5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:tag="accessorio" />
</GridLayout>

<!-- sottogriglia per la categoria C -->

<GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/categoriaC1"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catc1"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaC2"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catc2"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaC3"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catc3"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaC4"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catc4"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaC5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:tag="accessorio" />
</GridLayout>

<!-- sottogriglia per la categoria D -->

<GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/categoriaD1"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catd1"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaD2"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catd2"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaD3"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catd3"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaD4"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:background="@drawable/catd4"
        android:tag="accessorio" />

    <ImageButton
        android:id="@+id/categoriaD5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:tag="accessorio" />
</GridLayout>
</GridLayout> 

并在JAVA代码中针对按钮单击执行此操作:

 LinearLayout anteprimaAvatarLinear= (LinearLayout)findViewById(R.id.anteprimaAvatarLinear);
 ImageView anteprimaAvatar= (ImageView) findViewById(R.id.anteprimaAvatar);
 anteprimaAvatarLinear.setVisibility(v.VISIBLE); 
 anteprimaAvatar..setVisibility(v.GONE);  // OR INVISIBLE