2016年7月23日 星期六

Layoutdemo


==============================================

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="match_parent"    android:orientation="vertical"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity">

    <!--Child views are drawn in a stack, with the most recently added child on top.-->    <FrameLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:padding="12dp"        android:background="#DDFFDD">

        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="onImageG1Click"            android:src="@drawable/cat" />

        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="onImageG1Click"            android:src="@drawable/mouse" />

        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="onImageG1Click"            android:src="@drawable/dog" />

    </FrameLayout>

    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:padding="12dp"        android:background="#DDDDFF"        android:orientation="horizontal">

        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="onImageG2Click"            android:src="@drawable/cat" />

        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="onImageG2Click"            android:src="@drawable/mouse" />

        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="onImageG2Click"            android:src="@drawable/dog" />
    </LinearLayout>

    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="12dp"        android:text="@string/text_btReset"        android:id="@+id/btReset"        android:layout_gravity="center_horizontal"        android:onClick="onResetClick" />

</LinearLayout>



=========================================================================
main.java

=========================================================================

public class MainActivity extends AppCompatActivity {

    List<View> views;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        views = new ArrayList<>();
    }

    // 被點擊的ImageView 會被當作參數傳遞給view... 呼叫setVisibility() 將其設定為隱藏...但乃占空間.... 
    將view 加入views內方便之後按下Reset 按鈕後還原成顯示狀態

    // the clicked image becomes invisible    public void onImageG1Click(View view) {
        view.setVisibility(View.INVISIBLE);
        views.add(view);
    }

    // the clicked image becomes invisible and doesn't take any space    public void onImageG2Click(View view) {
        view.setVisibility(View.GONE);
        views.add(view);
    }

    // all the invisible images become visible    public void onResetClick(View view) {
        if (views != null && views.size() > 0) {
            for (View v : views) {
                v.setVisibility(View.VISIBLE);
            }
        }
    }
}

沒有留言:

張貼留言