【问题标题】:Unable to dynamically post imageview无法动态发布 imageview
【发布时间】:2016-07-20 03:56:09
【问题描述】:

每当我尝试从我的应用程序中的 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);    

    }
}

【问题讨论】:

  • 您正在向 TableLayout 中添加行,所以不是垂直显示,而不是水平显示吗?
  • 好吧,即使是垂直的也可以……但是屏幕上只有一张图像是问题所在……顺便说一句,谢谢您的回复:)
  • 不运行代码,我看不出你在做什么有什么问题。当您说“一张图片”时,您确定您使用的是不同的网址吗?
  • 是的,当然....实际上第一张图片与下一张重叠...当我尝试使用 for 循环做同样的事情时...nd 使用 urls[i ] 而不是 0 或 1 ...我的应用程序崩溃了..
  • 您能否在您的activity_main.xml 文件中添加edit,好吗?

标签: android android-layout picasso


【解决方案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

【讨论】:

  • 嘿,我猜对了……也许只是 ma 模拟器的问题……我在手机上运行它正在加载两个图像……非常感谢……我很感激它.. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多