ImageView上的TextView无法在Fragment中工作

时间:2014-08-11 12:27:42

标签: android

我想将文本放在图像视图上并尝试将其显示在片段中。当我使用活动处理相同的任务时,我得到了输出。我尝试使用片段时失败了。任何人都可以告诉我如何以片段形式做到这一点。

    @Override
    public View getView(int position,View view,ViewGroup parent){
    if(view ==null){LayoutInflater     inflater=LayoutInflater)getContext().getSystemService(Service.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.list_item, parent, false);
    holder.imgfile =(ImageView)view.findViewById(R.id.image);
    holder.title=(TextView) view.findViewById(R.id.txt);
    holder.title.setText(custgridfragdata.get(positiong).get("channelname"));
    Typeface font=Typeface.createFromAsset(getContext().getAssets(),"MTCORSVA.TTF");
    holder.title.setTypeface(font);
    view.setTag(holder);
    }else{holder=(ViewHolder)view.getTag();}
    return view;}

我在定义本地适配器类getView中定义它们。在该适配器类中,我定义了布局元素。我在片段中使用GridView。我的代码如下所示:

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
    {
    GView =  new GridView(getActivity());
    Configuration config = getResources().getConfiguration();
    if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
    GView.setNumColumns(3);
    }
    gridfragdata=MainActivity.userselected; 
    GView.setAdapter(new CustomGrid(getActivity(),R.layout.grid_item, gridfragdata));
    return GView;
    } 

这里我在ImageView上使用ImageView和TextView定义了一个布局。我想在图像上显示文字。当我使用GridView的fragements时,我想从布局中获取它们并以片段形式显示。为此,我在GetView()方法的适配器类中定义了一个Adapter类,我从xml获取了元素。我将Apater设置为GridView对象为;

GView.setAdapter(new CustomGrid(getActivity(),R.layout.grid_item, gridfragdata));

在片段中,因为我们不需要为GridView定义Xml

GView.setAdapter(new CustomGrid(getActivity(),R.layout.grid_item, gridfragdata)); 

此处R.layout.grid_item是包含图像和文本的Xml文件

<FrameLayout 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:background="@drawable/bg_18" >

    <ImageView
        android:id="@+id/grid_image"
        android:layout_width="140dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
         android:background="@color/green"
        android:paddingLeft="5dp"
        android:paddingRight="40dp" />

    <TextView
        android:id="@+id/grid_text"
        android:layout_width="130dp"
        android:layout_height="25dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="15dp"
        android:background="@color/cream"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:textAppearance="@style/GridFont" />

</FrameLayout>

2 个答案:

答案 0 :(得分:0)

onCreateView(...)方法中,需要使用View对象,例如

    View view = inflater.inflate(R.layout.browsematerialdocument, null);

    ImageView imgV= (ImageView ) view
            .findViewById(R.id.imgv_id);

答案 1 :(得分:0)

我在getView中定义它们来定义本地适配器类,并在该适配器类中定义布局元素。并在片段中使用GridView。我的代码是:

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
    GView =  new GridView(getActivity());
    Configuration config = getResources().getConfiguration();
    if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
    GView.setNumColumns(3);
    }
    gridfragdata=MainActivity.userselected; 
    GView.setAdapter(new CustomGrid(getActivity(),R.layout.grid_item, gridfragdata));
    return GView;
}