我正在开发一个示例应用程序。我想列出在RelativeLayout中显示的数据。但是它显示不正确。
我想查看:
但这是视图:
我的xml代码:
<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>
我的视图功能代码:
@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;
}
}
}