【问题标题】:Create an ImageView and put it in a RelativeLayout创建一个 ImageView 并把它放在一个 RelativeLayout
【发布时间】:2013-11-08 21:04:49
【问题描述】:

我需要在带有 LinearLayout 的 ScrollView 中显示一些图像,我在 xml 中有这个。

    <HorizontalScrollView 
        android:id="@+id/HorizontalS"
        android:layout_below="@+id/button1"
        android:layout_width="300dp"
        android:layout_height="350dp">

        <LinearLayout 
            android:layout_width="300dp"
            android:layout_height="350dp"
            android:orientation="horizontal"
            android:id="@+id/Relative1">
        </LinearLayout>

    </HorizontalScrollView>

这个在 .java 文件中

    LinearLayout oneLayout = (LinearLayout)findViewById(R.id.Relative1);

    //CREA EL IMAGEVIEW
    ImageView mImage = new ImageView(this);

    //CREA EL DRAWABLE Y LO SETEA EN EL IMAGEVIEW
    try {
        InputStream ims = getAssets().open("mc/mc1.jpg");
        Drawable d = Drawable.createFromStream(ims, null);
        mImage.setImageDrawable(d);
    }
    catch(IOException ex) {
        return;
    }

    //INGRESA LOS PARAMETROS Y LA IMAGEN AL LAYOUT
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    oneLayout.addView(mImage, lp);

这不起作用,程序不显示图像:c

感谢您的帮助。

【问题讨论】:

  • 您是否尝试过调试代码以查看实际发生的情况?
  • 是的,但我不明白调试过程

标签: android android-layout android-linearlayout horizontalscrollview


【解决方案1】:

您为什么不转向其他解决方案?

据我所知,您需要在一些图像之间滑动,以便您可以使用 ViewFlipper 执行此操作并在 xml 布局中定义所有图像

【讨论】:

  • 因为我需要动态地从文件夹中获取图像。你明白吗?
【解决方案2】:
// try this
LinearLayout oneLayout;
ImageView mImage;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         oneLayout = (LinearLayout)findViewById(R.id.Relative1);

        //CREA EL IMAGEVIEW
        mImage = new ImageView(this);
        mImage.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mImage.setAdjustViewBounds(true);
        mImage.setScaleType(ImageView.ScaleType.FIT_XY);

        //CREA EL DRAWABLE Y LO SETEA EN EL IMAGEVIEW
        try {
            InputStream ims = getAssets().open("mc/mc1.jpg");
            Drawable d = Drawable.createFromStream(ims, null);
            mImage.setImageDrawable(d);
        }
        catch(IOException ex) {
            return;
        }
        oneLayout.addView(mImage);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多