我正在开发一个示例应用程序。我想在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;
}
感谢您的帮助。
答案 0 :(得分:2)
您应该在getView中夸大布局,而不是内容。
View rowView = inflater.inflate(R.id.content, parent, false);
应该是
View rowView = inflater.inflate(R.layout.layout_name, parent, false);
修改强> 您不应将标题放在每个列表项中。在列表项中,只需放置文本视图并将标题放在主布局中。
主要布局:
<RelativeLayout
....
<!-- Header layout -->
<ListView />
</RelativeLayout>
列出项目布局:
<RelativeLayout
...
<TextView />
</RelativeLayout>
之后只需输入inflater layout.list_item
答案 1 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
<强> 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;
}
}
}