2016-08-04 56 views
3

In meiner XML-Datei setze ich jeden kleinen Kreis (ImageView) auf die Größe von 25% des Bildschirms mit einer linearen Ansicht und layout_weights.
Hier ist ein Screenshot von meinem Handy (wie es aussehen soll):Wie layout_weight im linearen Layout zu beheben?

enter image description here

Auf meiner Tablette es 25% des Bildschirms wird nicht die Größe ändern, und das ist, wie es aussieht :

enter image description here

My Code besteht aus drei LinearLayouts von 1.
Hier jeweils ImageViews mit Gewichten von 0,25 ist der Code:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    xmlns:ads="http://schemas.android.com/apk/res-auto"> 
    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/imageView12" 
     android:src="@drawable/circle_big" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

    <LinearLayout 
     android:id="@+id/wrapper" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1.0" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     > 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.03125"> 
     </FrameLayout> 
     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:id="@+id/imageView12" 
      android:src="@drawable/circle_white" 
      android:onClick="changeToScreenSelectLayout" 
      android:scaleType="fitCenter" 
      android:adjustViewBounds="true" 
      /> 
     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.09375"> 
     </FrameLayout> 
     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:id="@+id/imageView12" 
      android:src="@drawable/circle_white" 
      android:onClick="changeToScreenSelectLayout" 
      android:scaleType="fitCenter" 
      android:adjustViewBounds="true" 
      /> 
     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.09375"> 
     </FrameLayout> 
     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:id="@+id/imageView12" 
      android:src="@drawable/circle_white" 
      android:onClick="changeToScreenSelectLayout" 
      android:scaleType="fitCenter" 
      android:adjustViewBounds="true" 
      /> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.03125"> 

     </FrameLayout> 


    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/wrapper" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1.0" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:rotation = "120" 
     > 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.03125"> 
     </FrameLayout> 
     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:id="@+id/imageView12" 
      android:src="@drawable/circle_white" 
      android:onClick="changeToScreenSelectLayout" 
      android:scaleType="fitCenter" 
      android:adjustViewBounds="true" 
      /> 
     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.4375"> 
     </FrameLayout> 
     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:id="@+id/imageView12" 
      android:src="@drawable/circle_white" 
      android:onClick="changeToScreenSelectLayout" 
      android:scaleType="fitCenter" 
      android:adjustViewBounds="true" 
      /> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.03125"> 

     </FrameLayout> 


    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/wrapper" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1.0" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:rotation = "60" 
     > 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.03125"> 
     </FrameLayout> 
     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:id="@+id/imageView12" 
      android:src="@drawable/circle_white" 
      android:onClick="changeToScreenSelectLayout" 
      android:scaleType="fitCenter" 
      android:adjustViewBounds="true" 
      /> 
     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.4375"> 
     </FrameLayout> 
     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.25" 
      android:id="@+id/imageView12" 
      android:src="@drawable/circle_white" 
      android:onClick="changeToScreenSelectLayout" 
      android:scaleType="fitCenter" 
      android:adjustViewBounds="true" 
      /> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.03125"> 

     </FrameLayout> 


    </LinearLayout> 

</RelativeLayout> 
+0

Die Verwendung eines PercentRelativeLayout kann helfen. –

+0

Soweit ich weiß, müssen Layout Gewichte ganze Zahlen sein –

+0

@ cricket_007 ** falsch **. Sie können Fließkommawerte sein. –

Antwort

1

Sie können eine PNG-Datei nicht als circle_white verwenden. Du könntest das technisch machen, wenn du das korrekte android: scaleType wünschst, aber es würde in schrecklich aliased Wiedergaben ergeben.

Ich würde zuerst Ihre vorhandenen PNGs (Von jedem Ressourcen-Bucket) löschen, da sie dafür nicht verwendet werden können. Dann erstellen Sie eine neue Datei in Zeichnungsdatei namens "circle_white.xml". Setzen Sie das ein, das wird einen Kreis ohne innere Größe zeichnen. I.E. es wird nur füllen, was auch immer Sie es rendern.

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <solid 
     android:color="#fff"/> 
</shape> 

Und dann mit dem auf diesen neu @ ziehbar/circle_white gesetzt Hintergrund mit einem src von circle_white zu einem HeightMatchesWidthView verwenden Image des ändern. Sie müssten sicherstellen, dass die Höhe der Ansichten der Breite entspricht, die ich in einer darunter liegenden Ansichtserweiterung erzielt habe. Um dies zu verwenden nur eine neue Klasse macht irgendwo und fügen Sie diese in. In XML-Referenz durch anstelle von Image tun com.whatever.HeightMatchesWidthView wo die com.whatever sind, was Paket, das Sie diese in.

public class HeightMatchesWidthView extends View { 
    public HeightMatchesWidthView(final Context context) { 
     super(context); 
    } 

    public HeightMatchesWidthView(final Context context, final AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public HeightMatchesWidthView(final Context context, final AttributeSet attrs, final int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { 
     int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); 
     setMeasuredDimension(width, width); 
    } 

    @Override 
    protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) { 
     super.onSizeChanged(w, w, oldw, oldh); 
    } 
} 

Statt von HeightMatchesWidthView können Sie stattdessen auch PercentRelativeLayout verwenden und diese verwenden, um sie zu quadrieren, und die richtige Größe, ohne View erweitern zu müssen, um Unterstützung zum Erzwingen von Quadrat hinzuzufügen.

+0

Es sind die weißen Kreise von den Bildern – sazzy4o

+0

Also ist es eine tatsächliche PNG-Datei? –

+0

Ja, es ist eine PNG-Datei von LDPI zu xxhdpi – sazzy4o