2016-09-24 72 views
0

我的目标是从布局夸大我的自定义视图布局气筒充气,我的自定义视图中的FrameLayout而不是

自定义视图布局:

<net.rhyboo.com.game_test.Piece xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="fitXY" 
    android:id="@+id/piece_view"> 

</net.rhyboo.com.game_test.Piece> 

用这个布局相关类:

package net.rhyboo.com.game_test; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

public class Piece extends ImageView { 

    public Piece(Context context, AttributeSet atrs) { 
     super(context,atrs); 
    } 

    public Piece(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 
} 

但是当我尝试夸大代码:

//get layout inflater 
LayoutInflater li=(LayoutInflater)activity.getSystemService(Service.LAYOUT_INFLATER_SERVICE); 
//parent view for my custom view 
FrameLayout gf =(FrameLayout)fragment.getView().findViewById(R.id.game_field); 
//inflation attempt 
Piece p=(Piece) li.inflate(R.layout.piece_view,gf); 

我得到了错误

java.lang.ClassCastException:android.widget.FrameLayout不能转换到net.rhyboo.com.game_test.Piece

为什么布局吹气产生的FrameLayout而类型我的观点是Piece,它是ImageView的子类?

+0

发布您的完整xml – Nikhil

+0

'片p =(片)li.inflate(R.layout.piece_view,gf);'这行似乎返回父视图,而不是一个里面。尝试在该视图上调用'findViewById',如'View view = li.inflate(R.layout.piece_view,gf); P片=(片)view.findViewById(R.id.piece_view);' – Vucko

回答

0

相反的:

//get layout inflater 
LayoutInflater li=(LayoutInflater)activity.getSystemService(Service.LAYOUT_INFLATER_SERVICE); 
//parent view for my custom view 
FrameLayout gf =(FrameLayout)fragment.getView().findViewById(R.id.game_field); 
//inflation attempt 
Piece p=(Piece) li.inflate(R.layout.piece_view,gf); 

尝试

//get layout inflater 
LayoutInflater li =(LayoutInflater)activity.getSystemService(Service.LAYOUT_INFLATER_SERVICE); 
//parent view for my custom view 
FrameLayout gf = (FrameLayout)fragment.getView().findViewById(R.id.item); 
//inflation attempt 
Piece p = li.inflate(R.layout.piece_view, null); 
gf.addView(p); 

编辑:要查看图片在原始大小这个参数添加到ImageView

android:scaleType="center" 
+0

谢谢!问题与父参数。实际上我不需要我的视图有父视图。不知道layoutInflater可以接受null作为第二个参数。 现在问题是我的ImageView有0宽度/高度,即使我设置非零大小的位图作为content.And后,我将它添加到显示列表它出现缩放。是否有办法让它与原始大小apear? –

+0

@СергейКоджа我编辑我的答案 – piotrek1543

+0

与scaleType =“中心” 我的位图跳转到屏幕的中心,当它应该在左上角。也可以在onLayout我可以看到实际位图大小时,视图大小1106x736:51x68 无论如何,我找到了设置大小的方法: LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(bmp.getWidth(),bmp.getHeight()); p.setLayoutParams(parms); –

0

我想你应该尝试在这两种方式:

  1. 如果您希望自定义视图将被添加到框架布局,然后使用

查看视图= inflater.inflate(R.layout.your_custom_layout,mFrameLayout,真正的);

  • 如果您不希望您的自定义视图将被添加到框架布局,然后使用
  • 查看视图= inflater.inflate(R.layout.your_custom_layout,父,假);

    在代码

    可能的校正:

    视图视图= li.inflate(R.layout.piece_view,GF);

    查看视图= inflater.inflate(R.layout.piece_view,gf);

    最后返回视图;


    根据看来你是错误的铸造框架布局到您的自定义布局在这里

    //通胀尝试 工件(P)=(件)li.inflate错误(R.layout。piece_view,GF);