【问题标题】:RelativeLayout margins inside a ListView are not displayed不显示 ListView 内的 RelativeLayout 边距
【发布时间】:2010-12-21 13:50:36
【问题描述】:

我目前正在使用一个 ListView,我用一个带有 RelativeLayout 的自定义适配器填充它。问题是没有为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)

【问题讨论】:

  • 您能否更新问题以显示 Java 代码您在哪里使用包含 RelativeLayout 的布局文件?谢谢!
  • 我刚刚在下面发布了代码......

标签: android


【解决方案1】:

如果您的边距仅适用于外边缘,请尝试在 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"
         />
    

    【讨论】:

      【解决方案3】:

      这里是适配器和主代码+主xml的代码

      文章片段代码:

      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>
      

      希望对你有帮助……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        • 2020-01-06
        • 2023-03-25
        • 1970-01-01
        • 2014-12-21
        • 1970-01-01
        相关资源
        最近更新 更多