1

使用 Array Adapter 进行膨胀 - 膨胀的布局没有填满我的全屏。

父视图组中的视图显示在我新膨胀的布局的底部。充气不适合全屏并导致图像中的显示问题。代码工作正常,但我的显示器有问题。请提出可能的解决方案。父 XML 也是一个膨胀的布局列表(数组适配器)。

笔记

  • 布局通过设置 attachRoot=false (Inflater.inflate(R.layout.chapterlayout, parent, false)) 正确膨胀。
  • 无法从 ArrayAdapter 处理 ViewGroup,因此无法删除父视图。

膨胀的 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/my_background"
android:orientation="vertical" 
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <TextView
        android:id="@+id/ENo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="90"
        android:text="1."
        android:textColor="#0000FF"
        android:textSize="15sp"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/EA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:paddingBottom="5sp"
        android:text="My Text"
        android:textColor="#0000FF"
        android:textSize="15sp"
        android:textStyle="bold" >
    </TextView>
</LinearLayout>

<TextView
    android:id="@+id/EE"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="My Text"
    android:textColor="#0000FF"
    android:textSize="15sp"
    android:textStyle="bold" >
</TextView>

</LinearLayout>

父 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:background="@drawable/my_background">

<TextView
    android:id="@+id/txt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="15sp"
    android:textColor="#0000FF"
    android:textStyle="bold"
    android:background="@drawable/my_background">
</TextView>

</LinearLayout>

JAVA代码

public class MyChapterAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public MyChapterAdapter(Context context, String[] values) {
    super (context, R.layout.main, values);
    this.context = context;
    this.values = values;
}

static class ViewHolder { 
    TextView ENo; 
    TextView EA; 
    TextView EE;    
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder; 
    if (convertView == null) { 
        LayoutInflater li = (LayoutInflater)   
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.chapterlayout, parent, false);
        holder = new ViewHolder(); 
        holder.ENo = (TextView) convertView.findViewById(R.id.ENo); 
        holder.EA = (TextView) convertView.findViewById(R.id.EA); 
        holder.EE = (TextView) convertView.findViewById(R.id.EE); 
        convertView.setTag(holder); 
   } else { 
       holder = (ViewHolder) convertView.getTag(); 
   } 

   reDesignLayout(position,  holder);
   return convertView; 

}

private void reDesignLayout(int position, ViewHolder h) {
    // TODO Auto-generated method stub

    String chapSplit[] =values[position].split("##");

       h.ENo.setText(chapSplit[0]+". "); 
       h.EA.setText(chapSplit[1]); 
       h.EE.setText(chapSplit[2]); 
    }
}
4

0 回答 0