2012-08-14 63 views
0

我得到這個代碼的問題:添加按鈕/ EditText上的相對佈局(膨脹)

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.list_mobile, parent, false); 
     TextView textView = (TextView) rowView.findViewById(R.id.label); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); 
     textView.setText(values[position]); 

     // Change icon based on name 
     String s = values[position]; 

     System.out.println("s = "+s); 
     System.out.println("Results = "+name[2].toString()); 
     for (int i = 0; i < name.length; i ++){ 
      if (s.equals(name[i])) { 
       imageView.setImageDrawable(loadImageFromURL("http://10.0.0.117/test/"+s+".jpg", "iOS")); 
      } 
     } 
     return rowView; 
    } 

這是我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="5dp" > 

    <ImageView 
     android:id="@+id/logo" 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="5dp" 
     android:contentDescription="@string/desc" 
     android:src="@drawable/windowsmobile_logo" > 
    </ImageView> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/logo" 
     android:layout_marginBottom="16dp" 
     android:layout_toRightOf="@+id/logo" 
     android:text="@+id/label" 
     android:textSize="20dp" /> 


    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" > 



     <Button 
      android:id="@+id/btn_zoeken" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_toRightOf="@+id/txt_zoeken" 
      android:text="@string/Zoeken" /> 

     <EditText 
      android:id="@+id/txt_zoeken" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ems="10" > 

      <requestFocus /> 
     </EditText> 
    </RelativeLayout> 

</RelativeLayout> 

我得到了這個問題:
我收到了一個清單,在屏幕的底部,它們應該始終是EditTextbutton。但是,當我加入這個佈局和啓動應用程序,它把EditTextbuttonrow,看圖片:

這是怎麼回事(壞的):

How it is now (bad)

這是應該的:

How it should be

我在佈局中RelativeLayout得到了RelativeLayout,因爲我希望它能解決這個問題,但事實並非如此。

請注意,我在相對佈局中創建了一個列表,所以我不使用ListView。
任何人都有解決方案或建議如何解決這個問題?

我希望我的問題很清楚。

感謝不已,Bigflow

編輯1:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View rowView = inflater.inflate(R.layout.inflater, parent, false); 
     TextView textView = (TextView) rowView.findViewById(R.id.label); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); 
     Button btn_zoeken = (Button) rowView.findViewById(R.id.button1); 
     textView.setText(values[position]); 

     // Change icon based on name 
     String s = values[position]; 

     System.out.println("s = "+s); 
     System.out.println("Results = "+name[2].toString()); 
     for (int i = 0; i < name.length; i ++){ 
      if (s.equals(name[i])) { 
       imageView.setImageDrawable(loadImageFromURL("http://10.0.0.117/test/"+s+".jpg", "iOS")); 
      } 
     } 
     return rowView; 
    } 
+2

您可以用'ListView的Footer'這一點​​。這會幫助你http://www.vogella.com/articles/AndroidListView/article.html#miscellaneous_headerfooter, – 2012-08-14 07:29:19

回答

1

使用您的按鈕(即btn_zoeken)和圖片(即txt_zoeken)在main.xml文件頁腳Mohasin Naeem suggested.And使用android:layout_alignParentBottom="true"

並將list_mobile.xml膨脹爲main.xml佈局文件。

所以你的list_mobile.xml應該看起來像這樣。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_toLeftOf="@+id/button1" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="Button" /> 

</RelativeLayout> 

其中btn_zoeken和txt_zoeken應該查看主屏幕的底部。 你的膨脹layout.xml應該如下。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="5dp" > 

<ImageView 
    android:id="@+id/logo" 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="5dp" 
    android:contentDescription="@string/desc" 
    android:src="@drawable/windowsmobile_logo" > 
</ImageView> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/logo" 
    android:layout_marginBottom="16dp" 
    android:layout_toRightOf="@+id/logo" 
    android:text="@+id/label" 
    android:textSize="20dp" /> 
</RelativeLayout> 

基本上同時充氣佈局需要兩個XML files.One是您main.xml(list_mobile.xml),另一種是你的充氣XML

+0

我的main.xml是我的'list_mobile.xml',我不得不說,對不起。 我現在會嘗試這個解決方案。 – Bigflow 2012-08-14 07:39:05

+0

我仍然無法工作...請幫助我,我有1佈局,嘗試2,但我只是不明白。 – Bigflow 2012-08-14 08:32:27

+0

請參閱我已更改我的帖子。讓我知道它是否有效? – Akshay 2012-08-14 08:57:38