2009-11-30 101 views
3

我目前正在使用一個ListView,我用RelativeLayout的自定義適配器填充。問題是RelativeLayout不顯示邊距。ListView中的RelativeLayout邊距不顯示

這裏是我的相對佈局聲明:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ArticleSnippet" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="6dip" 
    android:layout_marginTop="10dip" 
    android:layout_marginLeft="10dip" 
    android:layout_marginRight="10dip" 
    android:background="@drawable/background_snippet" > 
    ... 
</RelativeLayout> 

ListView控件聲明:

<ListView 
    android:id="@+id/ArticleSnippets" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:divider="#fff" 
    android:dividerHeight="0sp"> 
</ListView> 

有什麼特別做的,使邊緣一個ListView裏面活躍?

在此先感謝您的幫助,

林特(Dusariez JF)

+0

您可以更新問題以顯示您使用包含RelativeLayout的佈局文件的Java代碼?謝謝! – CommonsWare 2009-11-30 16:52:49

+0

我剛剛在下面發佈代碼.... – 2009-12-01 08:35:12

回答

0

下面是該適配器和主代碼+主XML代碼

ArticleSnippet代碼:

package org.antihero.reader; 

import java.util.List; 

import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class ArticleSnippetAdapter extends BaseAdapter { 

    private List<RssMessage> elements; 
    private Context mContext; 

    public ArticleSnippetAdapter(Context mContext, List<RssMessage> elements) { 
     this.mContext = mContext; 
     this.elements = elements; 
    } 

    // TODO refactor this to limit number of views having an array ... 
    public View getView(int position, View convertView, ViewGroup parent) { 

     RelativeLayout rowLayout; 
     final RssMessage message = elements.get(position); 

     if (convertView == null) { 
      rowLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.article_snippet, parent, false); 
     } else { 
      rowLayout = (RelativeLayout) convertView; 
     } 

     TextView title = (TextView) rowLayout.findViewById(R.id.ArticleTitle); 
     title.setText(message.getTitle()); 

     TextView intro = (TextView) rowLayout.findViewById(R.id.ArticleIntro); 
     intro.setText(message.getDescription()); 

     Log.i("antiheroReader", "ArticleSnippetAdapter::getView();"); 

     return rowLayout; 
    } 

    public int getCount() { 
     Log.i("antiheroReader", "ArticleSnippetAdapter::getCount(); "+elements.size()); 
     return elements.size(); 
    } 

    public Object getItem(int position) { 
     Log.i("antiheroReader", "ArticleSnippetAdapter::getItem();"); 
     return elements.get(position); 
    } 

    public long getItemId(int position) { 
     Log.i("antiheroReader", "ArticleSnippetAdapter::getItemId();"); 
     return position; 
    } 

    public void add(RssMessage element) { 
     Log.i("antiheroReader", "ArticleSnippetAdapter::add();"); 
     elements.add(element); 
    } 

    public void empty() { 
     Log.i("antiheroReader", "ArticleSnippetAdapter::empty();"); 
     elements.clear(); 
    } 
} 

主代碼:

package org.antihero.reader; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 
import org.antihero.reader.RssMessage; 
import org.antihero.reader.AndroidSaxFeedParser; 

public class AntiheroReader extends Activity { 
    private ProgressDialog myProgressDialog = null; 
    private List<RssMessage> messages; 
    private ArticleSnippetAdapter articleSnippetAdapter; 
    private ListView articleSnippetView; 

    private Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      Log.i("antiheroReader", "handleMessage"); 
      articleSnippetAdapter.notifyDataSetChanged(); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     messages = new ArrayList<RssMessage>(); 
     articleSnippetAdapter = new ArticleSnippetAdapter(this, messages); 
     articleSnippetView = (ListView) findViewById(R.id.ArticleSnippets); 
     articleSnippetView.setAdapter(articleSnippetAdapter); 

     OnClickListener myProgressBarShower = new View.OnClickListener() {   
      @Override 
      public void onClick(View v) { 
       // myProgressDialog is null. 
       Log.i("antiheroReader", AntiheroReader.this.toString()); 
       myProgressDialog = ProgressDialog.show(AntiheroReader.this, "Please wait...", "Loading data ...", true, false);  
       Log.i("antiheroReader", myProgressDialog.toString()); 

       // Need to run stuff in the thread or the dialog isn't appearing 
       new Thread() { 
        public void run() { 
         URL feedUrl; 
         InputStream feedStream; 
         try{ 
          if(myProgressDialog.isShowing()) { 
           Log.i("antiheroReader", "Dialog show"); 
          } else { 
           Log.i("antiheroReader", "Dialog NOT show"); 
          } 

          try { 
           feedUrl = new URL("http://192.168.56.1/index.xml"); 
           //feedUrl = new URL("http://www.lalibre.be/rss/"); 
          } catch (MalformedURLException e) { 
           throw new RuntimeException(e); 
          } 
          try { 
           feedStream = feedUrl.openConnection().getInputStream(); 
           //Toast.makeText(AntiheroReader.this, feedStream.toString(), Toast.LENGTH_LONG).show(); 
          } catch (IOException e) { 
           throw new RuntimeException(e); 
          } 

          Log.i("antiheroReader", "ProgressDialog start parsing"); 

          AndroidSaxFeedParser feed = new AndroidSaxFeedParser(feedStream); 
          List<RssMessage> messages = feed.parse(); 
          articleSnippetAdapter.empty(); 

          myProgressDialog.dismiss(); 
          Log.i("antiheroReader", "ProgressDialog OFF"); 

          int count = messages.size(); 
          for(int i=0; i<count; i++) { 
           articleSnippetAdapter.add(messages.get(i)); 
           Log.i("antiheroReader", messages.get(i).getTitle()); 
           //Toast.makeText(AntiheroReader.this, messages.get(i).getTitle(), Toast.LENGTH_LONG).show(); 
          } 

          handler.sendEmptyMessage(0); 

         } catch (Exception e) { } 
         // Dismiss the Dialog 
         myProgressDialog.dismiss(); 
        } 
       }.start(); 
      } 
     }; 

     Button buttonHome = (Button) findViewById(R.id.ButtonHome); 
     buttonHome.setOnClickListener(myProgressBarShower); 

    } 
} 

main.xml中

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
     <!-- Logo --> 
     <ImageView 
      android:id="@+id/LogoLaLibre" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/logo_lalibre"> 
     </ImageView> 
     <!-- Date --> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/app_name" /> 
    </LinearLayout> 

    <HorizontalScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="none" 
     android:background="#770037"> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <Button 
       android:id="@+id/ButtonHome" 
       style="@style/MenuButtonHome" 
       android:layout_width="22px" 
       android:layout_height="wrap_content" 
       android:text="@string/section_home"/> 

      <Button 
       android:id="@+id/ButtonActu" 
       style="@style/MenuButtonActu" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/section_actu"/> 

      <Button 
       android:id="@+id/ButtonEconomie" 
       style="@style/MenuButtonEconomie" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/section_economie"/> 

      <Button 
       android:id="@+id/ButtonCulture" 
       style="@style/MenuButtonCulture" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/section_culture"/> 

      <Button 
       android:id="@+id/ButtonSport" 
       style="@style/MenuButtonSport" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/section_sports"/> 

      <Button 
       android:id="@+id/ButtonSociete" 
       style="@style/MenuButtonSociete" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/section_societe"/> 

     </LinearLayout> 
    </HorizontalScrollView> 

    <!-- Snippets prototype --> 
    <ListView 
     android:id="@+id/ArticleSnippets" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:divider="#fff" 
     android:dividerHeight="0sp"> 
    </ListView> 

</LinearLayout> 

最後的RelativeLayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ArticleSnippet" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="6dip" 
    android:layout_marginTop="10dip" 
    android:layout_marginLeft="10dip" 
    android:layout_marginRight="10dip" 
    android:background="@drawable/background_snippet_padding"> 

    <TextView 
     android:id="@+id/ArticleBreadCrumb" 
     android:layout_marginLeft="6dip" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/ArticlePicture" 

     android:textSize="12sp" 
     android:textStyle="bold" 
     android:textColor="#000" 

     android:text="Actu - Politique" /> 

    <TextView 
     android:id="@+id/ArticleTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/ArticleBreadCrumb" 
     android:layout_alignLeft="@id/ArticleBreadCrumb" 

     android:textSize="12sp" 
     android:textStyle="bold" 
     android:textColor="#000" 

     android:text="Test général en sciences, histoire et géo" /> 

    <TextView 
     android:id="@+id/ArticleIntro" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/ArticlePicture" 
     android:layout_alignLeft="@id/ArticlePicture" 

     android:textSize="10sp" 
     android:textColor="#000" 

     android:text="Semaine particulière pour les élèves de deuxième et cinquième primaire ainsi que de deuxième secondaire. La Communauté française leur a en effet concocté une évaluation externe non certificative en éveil scientifique et éveil historique et géographique" /> 

    <ImageView 
     android:id="@+id/ArticlePicture" 
     android:layout_width="80sp" 
     android:layout_height="56sp" 
     android:background="@drawable/pict_article"> 
    </ImageView>     
</RelativeLayout> 

我希望它能幫助....

2

我有你同樣的問題與自定義適配器相同的情況下,我的。 我使用不同類型的項目,其中大部分都有LinearLayout,因爲它們很簡單。 如果我在RelativeLayout中設置邊距或包含我的文本視圖的頂部/底部邊距,那麼使用RelativeLayout的不起作用。 所以我做了這個簡單的解決方法,它的功能非常完美:我將佈局中的邊距設置爲0(或者,如果您願意,可以使用textview)。 後來我加入了高度的簡單的觀點,我想保證金:)

例如爲:

<View 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/textview_listitem_spacer" 
    android:layout_width="fill_parent" 
    android:layout_height="5dip" 
    android:layout_below="@+id/textview_listitem_descr" 
    android:layout_toLeftOf="@+id/textview_listitem_cbox" 
    /> 
+0

簡單但有效 – 2012-01-18 00:46:20

5

如果你的利潤率僅僅適用於外邊緣,嘗試在RelativeLayout的填充來代替。

+0

簡單而正確的解決方案。謝謝 – 2013-07-03 10:15:22