2015年7月7日 星期二

Android-Increase multi ImageView on View

未整理

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <LinearLayout
        android:id="@+id/inhorizontalscrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

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

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"
                    android:visibility="gone" />

            </LinearLayout>
        </HorizontalScrollView>

    </LinearLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

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

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"
                    android:visibility="gone" />

            </LinearLayout>
        </ScrollView>

    </LinearLayout>

</LinearLayout>

java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    Button addinHorizontalScrollView, addinScrollView;
    LinearLayout inHorizontalScrollView, inScrollView;

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

        
        inHorizontalScrollView = 
                (LinearLayout)findViewById(R.id.inhorizontalscrollview);
        inScrollView = 
                (LinearLayout)findViewById(R.id.inscrollview);

        addinHorizontalScrollView = (Button)findViewById(R.id.button1);
        addinHorizontalScrollView.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                addImageView(inHorizontalScrollView);
            }});

        addinScrollView = (Button)findViewById(R.id.button2);
        addinScrollView.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                addImageView(inScrollView);
            }});

    }

    private void addImageView(LinearLayout layout){
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.drawable.ic_launcher);
        layout.addView(imageView);
    }

}

如果要設示ImageView的長寬
.
.
.
// 設置元件 "寬度"、"高度"
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(300, 500);
// 設置元件間距 "左"、"上"、"右"、"下"
params.setMargins(50, 5, 5, 5);
layout.addView(imageView, params);


來源 : 連結
如果要採用自己的介面 : LayoutInflater.inflate方法解析

沒有留言:

張貼留言