-2

Ich versuche, 6 Bilder als ImageButtons hinzuzufügen, aber diese Bilder sind vertikal gestreckt. Siehe das Bild unten:Gestreckte ImageViews in LinearLayout

enter image description here

Wie Sie im obigen Bild sehen können alle imagebutons vertikal gestreckt. Ich habe sie in drawable-xxxhdpi platziert. Die Auflösung jedes Bildes beträgt 512x512. Ich habe auch versucht, sie in mipmap-xxxhdpi oder xxhdpi, aber keinen Unterschied zu platzieren. Hier ist mein Code:

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

Irgendwelche Vorschläge, um sie normale nicht gestreckt aussehen? Alle diese Bilder sind im PNG-Format.

+0

fix Größen in Bildansicht: width = 512 und height = 512 –

+0

versuchen Entfernen android: Scaletype = "fitXY" –

Antwort

0

Bitte versuchen Sie diese aktualisierte Code löst:

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

Ich habe Ihren Code ausprobiert, aber jetzt sind die Bilder eingeklemmt. Siehe das Bild https://s16.postimg.org/fqpo8bbmd/Screenshot_from_2016_12_01_19_47_11.png Hier sind die Symbole, die ich verwende https://drive.google.com/file/d/0B32HX5u0sXk2Y212UWhFYjM5dnc/view?usp=sharing –

+0

ja. ... Ich habe Ihre Bilder heruntergeladen und ausgearbeitet. Ich habe versucht, mit Raster-Ansicht programmatisch sehr wahrscheinlich habe ich eine Lösung, wenn Sie mit Rasteransicht ok sind, werde ich die Antwort posten, nachdem Sie es basierend auf Ihrer Anforderung anpassen – HsRaja

+0

Ja, bitte posten Sie den Code dass du es versucht hast. Es ist mir egal, ob es Gridview ist. Danke! –

0

Haupt Täter ist android:scaleType="fitXY"

ändern es mit

android:scaleType="centerInside" 



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

Ich schlage vor, Sie zu Entfernen Sie das gesamte Gewicht für alle Layout und einfach Verwenden Sie nur Linner-Layout für alle Bilder

<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

Danke, dass Sie mir geholfen haben. Zuerst habe ich versucht, scaleType = "centerInside" zu ersetzen, aber es hat nichts getan. Dann habe ich deinen Code ausprobiert, aber alle Bilder sind jetzt mehr vertikal gestreckt. Siehe das Bild https://s17.postimg.org/xnt9gbuhr/Screenshot_from_2016_12_01_19_38_45.png Hier sind die Symbole, die ich verwendet habe https://drive.google.com/file/d/0B32HX5u0sXk2Y212UWhFYjM5dnc/view?usp=sharing –

+0

Probieren Sie einfach andere Optionen in scaleType Attribut wie Center usw. Immer noch nichts passieren, die nur das entfernen –

+0

Ich habe das der Grund ist Wight entfernen alle Android: GewichtSumme = "3" und Android: layout_weight = "1" und ändern android: layout_width = "0DP" zu android: layout_width = "wrap_content" aber wenn Sie das tun Bildschirm ist nicht in 3 Teile unterteilt –

0

Ich habe mein Problem gelöst. Hier ist der Code

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

Dank an alle, die mir geholfen, mein Problem zu lösen :)