无法动态发布imageview

时间:2016-03-31 15:33:35

标签: android android-layout picasso

每当我尝试从我的应用程序中的url加载图像时...只显示一个图像,但我希望两个图像像列一样水平显示...请帮助我。我希望两个图像都是一个接一个地同时显示。

这是我的主要活动:

package com.example.hp.gotcha;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;

import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

    public String[] urls;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        urls = new String[]{"url1...","url2...",};
        TableLayout table = (TableLayout) findViewById(R.id.table);

        TableRow tr = new TableRow(this);
        TableRow.LayoutParams lay = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
        ImageView image = new ImageView(this);
        Picasso.with(this).load(urls[0]).into(image);
        tr.addView(image, lay);
        table.addView(tr);

        TableRow tr1 = new TableRow(this);
        TableRow.LayoutParams lay1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
        ImageView image1 = new ImageView(this);
        Picasso.with(this).load(urls[1]).into(image1);
        tr.addView(image1, lay1);
        table.addView(tr1);    

    }
}

1 个答案:

答案 0 :(得分:1)

你可以添加你的布局xml吗?

我能够使用此布局activity_main.xml

成功测试您的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="rocks.throw20.myapplication.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <TableLayout
    android:id="@+id/table"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</TableLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

使用与MainActivity完全相同的代码,这就是它的外观。

Loading images into TableRows