-2

我试图添加6个图像作为ImageButtons,但这些图像是垂直拉伸的。见下图:在LinearLayout中拉伸ImageViews

enter image description here

,你可以在上面的图片中看到的所有imagebutons被垂直拉伸。我已经把它们放在drawable-xxxhdpi中。每张图片的分辨率为512x512。我也尝试将它们放在mipmap-xxxhdpi或xxhdpi中,但没有区别。这里是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#DCDCDC" 
tools:context="MainActivity"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="15dp" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="3.4"> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.1" /> 

     <ImageButton 
      android:id="@+id/radio_channel_1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/background_radio_channel_one" 
      android:scaleType="fitXY" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.1" /> 

     <ImageButton 
      android:id="@+id/radio_channel_2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/background_radio_channel_two" 
      android:scaleType="fitXY" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.1" /> 

     <ImageButton 
      android:id="@+id/radio_channel_3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/background_radio_channel_three" 
      android:scaleType="fitXY" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.1" /> 
    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:orientation="horizontal" 
     android:weightSum="3.4"> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.1" /> 

     <ImageButton 
      android:id="@+id/radio_channel_4" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/background_radio_channel_four" 
      android:scaleType="fitXY" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.1" /> 

     <ImageButton 
      android:id="@+id/radio_channel_5" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/background_radio_channel_five" 
      android:scaleType="fitXY" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.1" /> 

     <ImageButton 
      android:id="@+id/radio_channel_6" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/background_radio_channel_six" 
      android:scaleType="fitXY" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.1" /> 
    </LinearLayout> 
</LinearLayout> 

任何建议,以使它们看起来不正常伸展? 所有这些图像都是PNG格式。

+0

固定尺寸的图像视图:宽度= 512和高度= 512 –

+0

尝试删除的android:scaleType =“fitXY” –

回答

0

请试试这个更新的代码:

//源码Sample.java

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.GridView; 


public class Sample extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sample); 
    GridView gridview = (GridView) findViewById(R.id.gridview); 
    gridview.setAdapter(new ImageAdapter(Sample.this)); 
} 
} 

//ImageAdapter.java

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

// Constructor 
public ImageAdapter(Context c) { 
    mContext = c; 
} 

public int getCount() { 
    return mThumbIds.length; 
} 

public Object getItem(int position) { 
    return null; 
} 

public long getItemId(int position) { 
    return 0; 
} 

// create a new ImageView for each item referenced by the Adapter 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView; 

    if (convertView == null) { 
     imageView = new ImageView(mContext); 
     imageView.setLayoutParams(new GridView.LayoutParams(170, 170)); 

     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 

    } else { 
     imageView = (ImageView) convertView; 
    } 
    imageView.setImageResource(mThumbIds[position]); 
    return imageView; 
} 

// Keep all Images in array 
public Integer[] mThumbIds = { 

    R.drawable.ic_radio_2, R.drawable.ic_radio_3, 
    R.drawable.ic_radio_4, R.drawable.ic_radio_5, 
    R.drawable.ic_radio_4, R.drawable.ic_radio_6, 

}; 
} 
+0

我已经试过你的代码,但现在图像捏。查看图片https://s16.postimg.org/fqpo8bbmd/Screenshot_from_2016_12_01_19_47_11.png 下面是我使用https://drive.google.com/file/d/0B32HX5u0sXk2Y212UWhFYjM5dnc/view?usp=sharing –

+0

的图标。 ...我下载了你的图片并解决了问题。我试着用网格视图以编程方式尝试,如果你对网格视图没问题,我最有可能得到一个解决方案。我将根据你的要求发布答案 – HsRaja

+0

是的,请发布代码你尝试过。我不在乎它是否为gridview。 谢谢! –

0

主要罪魁祸首android:scaleType="fitXY"

android:scaleType="centerInside" 



scaleType="fitXY"` means image Stretch to its all corners 
scaleType="centerInside" means place at center of parent 

更改它,我建议你删除所有重量为所有的布局,仅只是使用Linner布局所有图像

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#DCDCDC"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="15dp" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="3" 
     android:layout_margin="5dp" 
     android:orientation="horizontal"> 

     <ImageButton 
      android:id="@+id/radio_channel_1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_margin="10dp" 
      android:background="@drawable/app_icon" 
      android:scaleType="centerInside" /> 


     <ImageButton 
      android:id="@+id/radio_channel_2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_margin="10dp" 
      android:background="@drawable/app_icon" 
      android:scaleType="centerInside" /> 



     <ImageButton 
      android:id="@+id/radio_channel_3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_margin="10dp" 
      android:background="@drawable/app_icon" 
      android:scaleType="centerInside" /> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:weightSum="3" 
     android:orientation="horizontal"> 


     <ImageButton 
      android:id="@+id/radio_channel_4" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_margin="10dp" 
      android:background="@drawable/app_icon" 
      android:scaleType="centerInside" /> 


     <ImageButton 
      android:id="@+id/radio_channel_5" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_margin="10dp" 
      android:background="@drawable/app_icon" 
      android:scaleType="centerInside" /> 

     <ImageButton 
      android:id="@+id/radio_channel_6" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_margin="10dp" 
      android:background="@drawable/app_icon" 
      android:scaleType="centerInside" /> 

    </LinearLayout> 
</LinearLayout> 

+0

谢谢你的帮助。首先,我尝试更换scaleType =“centerInside”,但它什么也没做。然后我尝试了你的代码,但现在所有的图像都更加垂直拉伸。查看图片 https://s17.postimg.org/xnt9gbuhr/Screenshot_from_2016_12_01_19_38_45。png 这里是我使用的图标 https://drive.google.com/file/d/0B32HX5u0sXk2Y212UWhFYjM5dnc/view?usp=sharing –

+0

只需尝试scaleType属性中的其他选项,如中心等仍然没有发生,只是删除 –

+0

我得到这个理由是要删除所有android:weightSum =“3”和android:layout_weight =“1”,并将android:layout_width =“0dp”更改为android:layout_width =“wrap_content” 但如果你这样做屏幕没有分成3部分 –

0

我解决了我的问题。下面是代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="MainActivity"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@mipmap/ic_radio_5_art" /> 
    </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:orientation="horizontal"> 

      <ImageButton 
       android:id="@+id/radio_channel_1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="1dp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/background_radio_channel_one" /> 

      <ImageButton 
       android:id="@+id/radio_channel_2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="1dp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/background_radio_channel_two" /> 

      <ImageButton 
       android:id="@+id/radio_channel_3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="1dp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/background_radio_channel_three" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <ImageButton 
       android:id="@+id/radio_channel_4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="1dp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/background_radio_channel_four" /> 

      <ImageButton 
       android:id="@+id/radio_channel_5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="1dp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/background_radio_channel_five" /> 

      <ImageButton 
       android:id="@+id/radio_channel_6" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:padding="1dp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/background_radio_channel_six" /> 

     </LinearLayout> 

    </LinearLayout> 

感谢所有那些谁帮我解决我的问题:)