2009-11-30 59 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