如何动态添加imageView以滚动具有线性布局的视图?

时间:2015-09-01 13:42:05

标签: android android-layout android-studio android-imageview

如何使用Java代码动态地将imageView添加到带有滚动视图的LinearLayout?我希望我的图像并排,但当检测到它的手机边缘时,添加新行但我似乎没有找到任何答案..他们所说的只是垂直或水平动态添加但没有换行..

有谁愿意帮助我? :(我是新来的。

我的XML代码如下:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/layoutWordsVert"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ffffff"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/layoutWordsHori"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/saya"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/border_darkgreen1"
                android:src="@drawable/me"
                android:padding="1dp"/>

        </LinearLayout>
    </LinearLayout>

</ScrollView>

预期视图:

|---------|---------|---------|
| image 1 | image 2 | image 3 |
|---------|---------|---------|
| image 4 | image 5 | image 6 |
|---------|---------|---------|

1 个答案:

答案 0 :(得分:0)

这是您的MainActivity.java的样子,

&#13;
&#13;
    use Cake\ElasticSearch\TypeRegistry;
    class PagesController extends AppController {
        public function index() {
            $english_pages = TypeRegistry::get ( 'EnglishPages' );
            $query = $english_pages->find ( 'all' );
         // $query = $query->getData();
         // $query->all () ;
         // $query->all ()->getResponse ()  );
         // json_decode (  stripslashes($query->all ()->getResponse ()->getData ()['message']) , true ) ;
         // echo json_last_error_msg ();
         // json_encode ( $query->all ()->getResponse ()->getData ()['message'] ) ;
       }
     }
&#13;
&#13;
&#13;

这将是你的GridView适配器,

&#13;
&#13;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements OnItemClickListener {

    ArrayAdapter<String> nosAdapter;
    GridView gridView;

    int images[] = { R.drawable.one, R.drawable.two, R.drawable.three,
            R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven,
            R.drawable.eight, R.drawable.nine, R.drawable.ten, R.drawable.zero,
            R.drawable.blank };

    MyGridViewAdapter adapter;

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

        gridView = (GridView) findViewById(R.id.GridView);
        adapter = new MyGridViewAdapter(this, images);
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
        Toast.makeText(this, "Item at pos "+pos+" clicked", Toast.LENGTH_SHORT).show();
    }

}
&#13;
&#13;
&#13;

最后是activity_main.xml

&#13;
&#13;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MyGridAdapter extends BaseAdapter {

    int[] images;
    Context context;

    public MyGridAdapter(Context context, int[] images) {
        this.context = context;
        this.images = images;
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

@Override
public View getView(int pos, View convertView, ViewGroup parent) {

    ImageView imageView;
    LinearLayout outerLayout;

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = (LinearLayout) inflater.inflate(R.layout.grid_my_images,null);
    }

    imageView = (ImageView) convertView.findViewById(R.id.ivImage);

    imageView.setImageResource(images[pos]);
    return convertView;
}
}
&#13;
&#13;
&#13;

布局文件网格,grid_my_images.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp" >

    <GridView
        android:id="@+id/GridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="3dp"
        android:numColumns="3"
        android:verticalSpacing="3dp" >
    </GridView>
</LinearLayout>