开发者

RelativeLayout margins inside a ListView are not displayed

开发者 https://www.devze.com 2022-12-13 00:33 出处:网络
I\'m currently using a ListView that I fill with a custom adapter with RelativeLayout\'s. The problem is that the margins are not displayed for the RelativeLayout.

I'm currently using a ListView that I fill with a custom adapter with RelativeLayout's. The problem is that the margins are not displayed for the RelativeLayout.

Here is my relative layout declaration :

<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> 

The ListView declaration :

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

Is there anything special to do to ma开发者_JAVA技巧ke the margin active inside a ListView ?

Thanks in advance for any help,

Lint (Dusariez JF)


If your margins just apply to the outer edges, try padding on the RelativeLayout instead.


I have your same problem for the same case with a custom adapter of mine. I use different type of items, and most of them have LinearLayout since they are simple. The ones using the RelativeLayout do not work, both if I set margins in RelativeLayout or as top/bottom margins of my textviews contained. So I did this simple workaround which works perfectly: I put margins to 0 in layout (or textview if you prefer). Then I added a simple view of the height that I wanted for margin :)

E.g.:

 <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"
     />


Here is the code for the adapter and main code + main xml

ArticleSnippet Code :

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();
    }
}

Main code :

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>

And finally the 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>

I hope it helps ....

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号