创建一个ImageView并将其放在RelativeLayout中

时间:2013-11-08 21:04:49

标签: android android-layout android-linearlayout horizontalscrollview

我需要在带有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

请求帮助。

2 个答案:

答案 0 :(得分:0)

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

从我能理解的你需要在一些图像之间滑动,这样,你可以用ViewFlipper完成这个并在xml布局中定义你的所有图像

答案 1 :(得分:0)

// 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);
}