android - Android中如何避免图片拉伸(stretch)

标签 android android-layout

我尝试以编程方式放置图像。但是当我放置图像时,它们会被拉伸(stretch)。我正在使用 Micromax Canvas 来查看我的申请。

这是我的代码

void populateData(){
        populate=(LinearLayout)findViewById(R.id.populate);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.weight=1.0f;

        for(int i=0;i<10;i++) {
            LinearLayout hr_populate=new LinearLayout(this);
            hr_populate.setOrientation(LinearLayout.HORIZONTAL);

            ImageView img=new ImageView(this);
            img.setBackgroundResource(R.drawable.spicejet);

            img.setLayoutParams(params);
            ImageView img1=new ImageView(this);
            img1.setBackgroundResource(R.drawable.book);
            img1.setLayoutParams(params);
            ImageView img2=new ImageView(this);
            img2.setBackgroundResource(R.drawable.arrow);
            img2.setLayoutParams(params);

            TextView text = new TextView(this);
            text.setText("BOS");
            text.setLayoutParams(params);
            TextView text1 = new TextView(this);
            text1.setText("SAN");
            text1.setLayoutParams(params);
            TextView text2 = new TextView(this);
            text2.setText("6h15m");
            text2.setLayoutParams(params);
            TextView text3 = new TextView(this);
            text3.setText("32,163");
            text3.setLayoutParams(params);

            LinearLayout book_div = new LinearLayout(this);
            book_div.setOrientation(LinearLayout.VERTICAL);
            book_div.setLayoutParams(params);

            book_div.addView(text3);
            book_div.addView(img1);

            hr_populate.addView(img);
            hr_populate.addView(text);
            hr_populate.addView(text1);
            hr_populate.addView(text2);
            hr_populate.addView(book_div);
            hr_populate.addView(img2);
            populate.addView(hr_populate);
        }
    }

这是我的 XML 代码

<ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/sortFlightLayouts">

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/populate"
            android:orientation="vertical">
        </LinearLayout>

    </ScrollView>

请建议我必须做出哪些改变才能摆脱这个问题

最佳答案

尝试将ImageView的scaleType设置为fitCenter。以下是不同比例类型的一个很好的示例: http://etcodehome.blogspot.com/2011/05/android-imageview-scaletype-samples.html

关于android - Android中如何避免图片拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17746054/

相关文章:

java - ArrayList.remove(int) 与不同线程中的 ArrayList.remove(Object)

java - 本例如何实现onchildclick监听?

java - 图像从 SD 卡加载

android - 等到异步任务完成它的工作

java - android.view.InflateException :Error Inflating Class

java - 在适配器和 fragment 中获取正确的上下文

android - Android Support Library的CardView在不同设备上的不同结果

Android studio 将本地化资源中的字符串(在 Textview 中)的一部分作为可点击的 url?

android - 为什么 Switch 在旧版 Android 上不显示?

android - imeOption ="actionNext"在 TextInputLayout 中不起作用