2017-06-07 71 views
0

我有一個佈局問題,我有一個自定義佈局,我在一個gridView中使用了一個複選框,假設它在imageView的右下角對齊。佈局的預覽顯示它看起來是正確的,但是當它加載到gridView時,它會將複選框移動到imageView的左上角。我已經嘗試在gridView中使圖像變小,並刪除GridView中對象之間的間距,但無濟於事。android自定義佈局沒有正確對齊gridview

這裏是我的GridView控件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/imgThumb" 
    android:layout_width="100dp" 
    android:layout_height="100dp"/> 

<CheckBox 
    android:id="@+id/chkImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignEnd="@+id/imgThumb" 
    android:layout_alignRight="@+id/imgThumb" 
    android:layout_alignBottom="@+id/imgThumb"/> 

</RelativeLayout> 

每個項目自定義佈局,這裏是GridView的佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:orientation="vertical"> 
<GridView 
    android:id="@+id/grdImages" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:numColumns="3" 
    android:verticalSpacing="5dp" 
    android:horizontalSpacing="5dp" 
    android:columnWidth="90dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center"/> 

<Button 
    android:id="@+id/btnSelect" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Select" 
    android:layout_marginTop="5dp"/> 

</LinearLayout> 

這裏是什麼,我想在GridView到商品的圖片看起來像,這也是什麼預覽顯示它看起來像Preview

這是什麼發生在實際gridView GridView

+0

我可以添加幾張圖片,如果它可以幫助任何人瞭解我的問題。感謝您的考慮。 – Shawnzey

+0

嘗試使用LinearLayout而不是RelativeLayout進行自定義佈局。 –

+0

我也試過,但我無法弄清楚如何讓複選框位於imageView的右下角。我試過重力=「頂部|結束」,但它把它放在imageView – Shawnzey

回答

0

試試這個代碼:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<RelativeLayout 
    android:layout_width="100dp" 
    android:layout_height="100dp"> 

<ImageView 
    android:id="@+id/imgThumb" 
    android:layout_width="100dp" 
    android:layout_height="100dp"/> 

<CheckBox 
    android:id="@+id/chkImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true"/> 
</RelativeLayout> 

+0

這對我很好,謝謝你! – Shawnzey

0

問題發生是因爲您沒有爲您的ImageView設置對齊方式。 只需爲ImageView設置alignParentTop = true即可。 然後,通過將其屬性layout_below指向ImageView的ID,將ImageView下方的複選框對齊。

或者,由於您已將ImageView設置爲固定大小,因此使用LinearLayout替換RelativeLayout的方向爲垂直。

+0

我剛剛嘗試了你的想法,並沒有效果。謝謝! – Shawnzey

+0

我明白了,您還必須爲父佈局設置固定大小。 –

+0

是的!這就是我不得不做的:)謝謝! – Shawnzey

0

嘗試添加:android:layout_marginStart="xdp"是您的案例的邏輯編號(無法看到任何圖像時不會說任何內容)複選框的xml bloq。

順便說一句,您應該使用constraint layouts來避免這些問題!而且,如果你已經是,你只是忘了限制複選框。

祝你好運。

+0

這對我有用,除非我擔心它會針對不同大小的手機有不同的對齊方式。我正在尋找更好,更簡單的解決方案。 – Shawnzey

0

這可能有所幫助。

custom_layout:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

<ImageView 
    android:id="@+id/imgThumb" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_gravity"center"/> 

<CheckBox 
    android:id="@+id/chkImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right|bottom"/> 

</FrameLayout> 
+0

這個工作效果很好,但對齊有點關閉,但感謝您的輸入,我一定會更多地瞭解FrameLayout以瞭解它們! – Shawnzey

+0

@Shawnzey歡迎您..請upvote,如果它幫助 –

0

這工作對我很好。

<ImageView 
android:id="@+id/imgThumb" 
android:layout_width="100dp" 
android:layout_height="100dp"/> 
<CheckBox 
android:marginTop="80dp" 
android:id="@+id/chkImage" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignRight="@+id/imgThumb"/> 
+0

這對我的工作,除非我擔心它會有不同的大小的電話不同的路線。我正在尋找更好,更簡單的解決方案。 – Shawnzey

+0

哦,我從來沒有想過支持各種各樣的屏幕。也許有人有你的問題更好的解決方案。 –

0

試試這個自定義Layout.This可以幫助你。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/lin" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/icons" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/gridImg" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginBottom="@dimen/_5sdp" 
     android:layout_marginTop="@dimen/_5sdp" 
     android:src="@mipmap/ic_launcher" /> 

    <CheckBox 
    android:id="@+id/chkImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right"/> 
</LinearLayout> 

</LinearLayout>