0

嗨我正在使用Gridview通過使用自定義適配器來顯示下面帶有文本的圖像。但問題是,當我獲取圖像和顯示,對準被改變,它不是proper.Here是我的代碼:如何在gridview android中使用完全相同的圖像和文字?

<?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"> 
<GridView 
    android:id="@+id/gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:numColumns="3" /> 
    </RelativeLayout> 

gridview.row:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
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:orientation="vertical" > 
<RelativeLayout 
android:id="@+id/RelativeLayout1" 
android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
<ImageView 
android:id="@+id/brand_picture" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/ic_launcher"/> 

<TextView 
android:id="@+id/brand_name" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/brand_picture" 
android:gravity="center_vertical" 
android:text="TextView" 
android:background="@color/blue" 
android:textColor="#FFFFFF" /> 

如何達到預期輸出相同的圖像尺寸和文字。

預期成果是:

enter image description here

但我的輸出看起來是這樣的。 enter image description here 謝謝。

+0

使用高度和寬度的ImageView不換內容 –

+0

也引力中心 –

+0

說例如:如果我把imageview設置爲80dp,80dp,它不會爲所有設置設備:( – Star

回答

0

在設置裹內容窗口小部件調整其高度/寬度參數來給定的內容size.so圖像視圖和文本視圖的

變化幅度從渦卷內容和設置相同的寬度兩者圖像視圖和文本視圖。

如:機器人:layout_width = 「70DP」

爲了讓文本在文本視圖中的CENTE你必須設置 安卓重力= 「中心」 XML:ROW

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
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:orientation="vertical" > 
<RelativeLayout 
android:id="@+id/RelativeLayout1" 
android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
android:background="#333333"> 
<ImageView 
    android:id="@+id/brand_picture" 
    android:layout_width="90dp" 
    android:layout_height="wrap_content" 
    android:background="#499999" 
    android:src="@drawable/ic_launcher"/> 

    <TextView 
    android:id="@+id/brand_name" 
    android:layout_width="90dp" 
    android:layout_below="@+id/brand_picture" 
    android:gravity="center" 
    android:text="AQUA LUNG" 
    android:background="#445657" 
    android:textColor="#FFFFFF" /> 
+2

,將無法適用於所有設備 – Star

+0

你試了一下嗎?它爲我工作。我已經發布了修改後的代碼 – droid

+1

他只是說它的工作方式,因爲它不會冷杉所有的屏幕尺寸,因爲固定寬度 –

0

嘗試this..hope它適用於ü..

<RelativeLayout 
     android:id="@+id/RelativeLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/brand_picture" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignRight="@+id/brand_name" 
      android:src="@drawable/ic_launcher" /> 

     <TextView 
      android:id="@+id/brand_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/brand_picture" 
      android:background="@color/blue" 
      android:gravity="center_vertical" 
      android:text="TextView" 
      android:textColor="#ffffff" /> 
0

添加屬性,

  android:layout_centerHorizontal="true" 

您的ImageView以及TextView。它會工作。

0

你可以試試這個佈局:

其實我已經取代你的線性佈局相對佈局,但它的作品就像你想要的。

<LinearLayout 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/brand_picture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" 
     android:layout_gravity="center" /> 

    <TextView 
     android:id="@+id/brand_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@color/blue" 
     android:text="TextView" 
     android:textColor="#FFFFFF" /> 
</LinearLayout> 

希望這會解決您的問題....

+0

只是試着讓我知道..是否適用於你... –

+0

這不會幫助,他仍然會得到相同的輸出 –

+0

仍然相同的輸出,看起來很小 – Star

0
<RelativeLayout 
android:id="@+id/RelativeLayout1" 
android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
android:background="#000000"> 
<ImageView 
    android:id="@+id/brand_picture" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher"/> 

    <TextView 
     android:id="@+id/brand_name" 
     android:layout_width="90dp" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/brand_picture" 
     android:layout_alignRight="@+id/brand_picture" 
     android:layout_below="@+id/brand_picture" 
     android:background="#445657" 
     android:gravity="center" 
     android:text="WHATEVER" 
     android:textColor="#FFFFFF" /> 

    </RelativeLayout> 

或者你可以嘗試

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
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:orientation="vertical" > 
<LinearLayout 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/brand_picture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" 
     android:layout_gravity="center" /> 

    <TextView 
     android:id="@+id/brand_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF" 
     android:text="TextView" 
     android:textColor="#FFFFFF" /> 
</LinearLayout> 
    </LinearLayout> 

Hopw這可以幫助你

相關問題