如何通过点击imageview获得布局? [英] how to get a layout on click of imageview?

查看:130
本文介绍了如何通过点击imageview获得布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好..我的布局中有 ImageView EditText 视图。我想在点击箭头的 ImageView 时添加不同的布局。

Hello.. I have a layout in which I have ImageView and EditText view. I want to add a different layout on click of ImageView of arrow.

ImageView 的点击事件后,它应如下所示。我怎么能得到这个?
请求帮助...

It should look like this after click event of ImageView. How can I get this?? Pleas help...

推荐答案

要获得完美答案,请添加您的xml文件和java文件源代码。

For perfect answer please add your xml file and java file source code.

示例

MainActivity.java

public class MainActivity extends Activity {

    LinearLayout linear1,linear2;
    ImageView imgArrow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linear1 = (LinearLayout) findViewById(R.id.linear1);
        linear2 = (LinearLayout) findViewById(R.id.linear2);
        imgArrow = (ImageView) findViewById(R.id.imgArrow);

        linear1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(linear2.getVisibility() == View.GONE){
                    linear2.setVisibility(View.VISIBLE);
                    imgArrow.setImageResource(R.drawable.up);
                }else{
                    linear2.setVisibility(View.GONE);
                    imgArrow.setImageResource(R.drawable.down);
                }
            }
        });
    }
}

activity_main.xml

<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="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/btnClick"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/tv1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.5"
            android:text="Additional Contact" />

        <ImageView
            android:id="@+id/imgArrow"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="0.2"
            android:src="@drawable/down" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:visibility="gone"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txt1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="User Name here" />


        <TextView
            android:id="@+id/txt2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Phone number here" />

        <Button
            android:id="@+id/btnone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Click Here" />
    </LinearLayout>

</LinearLayout>

此处向下和向上是箭头的图像文件。
所以将两个箭头文件放在drawable文件夹中。

Here down and up is image file for arrow. so put that both arrow file in drawable folder.


这篇关于如何通过点击imageview获得布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆