-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> 

感謝所有那些誰幫我解決我的問題:)