本文介绍了在RelativeLayout的列表项视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个示例应用程序。我想列出RelativeLayout的数据显示。但它不能准确显示。

I am developing a sample application . I want to list data show in RelativeLayout. But it is not show accurately.

我要查看:

但它认为:

我的XML code:

My xml code:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".MainNews" >

<!-- aligned to top -->

<RelativeLayout
    android:id="@+id/topeFixContent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#FC9"
    android:gravity="center" >

    <FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:background="#DCF4FC" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="46dp"
            android:layout_height="30dp"
            android:layout_gravity="top|right"
            android:contentDescription="@string/imgLogo"
            android:scaleType="center"
            android:src="@drawable/logo" />

        <Button
            android:id="@+id/menuButton"
            android:layout_width="46dp"
            android:layout_height="32dp"
            android:background="@drawable/menu" />
    </FrameLayout>
</RelativeLayout>

<!-- Header aligned to top -->

<RelativeLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_below="@id/topeFixContent"
    android:background="#FA9"
    android:gravity="top"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp" >

    <TextView
        android:id="@+id/hedderText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        />

</RelativeLayout>

<!-- Content below header  -->

<RelativeLayout
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="center" >

    <TextView
        android:id="@+id/bdNewsTex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="XX" />
</RelativeLayout>

</RelativeLayout>

我的查看功能code:

My View Function Code:

        @Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(context.LAYOUT_INFLATER_SERVICE);
    //View rowView = inflater.inflate(R.id.content, parent, false); 
                View rowView = inflater.inflate(R.layout.activity_main_news, parent, false);    
    TextView tv = (TextView) rowView.findViewById(R.id.bdNewsTex);      
    tf = Typeface.createFromAsset(context.getAssets(), "solaimanlipinormal.ttf");

    tv.setTypeface(tf);
    tv.setText(Html.fromHtml(items.get(position).getTitle()));      
    return rowView;
}

感谢您的帮助。

推荐答案

试试这个办法,希望这将帮助你解决你的问题。

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/topeFixContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FC9000"
        android:gravity="center_vertical" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#DCF4FC">
            <Button
                android:id="@+id/menuButton"
                android:layout_width="46dp"
                android:layout_height="32dp"
                android:background="@drawable/menu" />

            <View
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="46dp"
                android:layout_height="30dp"
                android:layout_gravity="top|right"
                android:contentDescription="@string/imgLogo"
                android:adjustViewBounds="true"
                android:src="@drawable/logo" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#FA9000"
        android:gravity="center">

        <TextView
            android:id="@+id/hedderText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"/>

    </LinearLayout>

    <ListView
        android:id="@+id/listNews"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/bdNewsTex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"/>
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity{

    private ListView listNews;
    private NewsAdapter newsAdapter;
    private ArrayList<String> newsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listNews = (ListView) findViewById(R.id.listNews);

        newsList = new ArrayList<String>();
        newsList.add("News 1");
        newsList.add("News 2");
        newsList.add("News 3");
        newsList.add("News 4");
        newsList.add("News 5");
        newsList.add("News 6");
        newsList.add("News 7");
        newsList.add("News 8");
        newsList.add("News 9");

        newsAdapter = new NewsAdapter(this,newsList);
        listNews.setAdapter(newsAdapter);
        listNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,newsList.get(position),Toast.LENGTH_SHORT).show();
            }
        });

    }


    class NewsAdapter extends BaseAdapter{

        private Context context;
        private ArrayList<String> newsList;

        public NewsAdapter(Context context,ArrayList<String> newsList){
            this.context = context;
            this.newsList = newsList;
        }

        @Override
        public int getCount() {
            return newsList.size();
        }

        @Override
        public Object getItem(int position) {
            return newsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView==null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null,false);
                holder.bdNewsTex = (TextView) convertView.findViewById(R.id.bdNewsTex);
                convertView.setTag(holder);
            }else{
                holder =(ViewHolder) convertView.getTag();
            }

            holder.bdNewsTex.setText(newsList.get(position));
            return convertView;
        }

        class ViewHolder {
            TextView bdNewsTex;
        }
    }

}

这篇关于在RelativeLayout的列表项视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:29