1

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

感谢您的帮助。

4

2 回答 2

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);
}
于 2013-11-09T04:09:15.570 回答
0

你为什么不转向另一个解决方案?

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

于 2013-11-08T21:12:59.080 回答