猿问

如何从 xml 中引用图像?

下面是程序中的 oncreate 方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    //ImageView img = (ImageView)controlInflater.
    LayoutParams layoutParamsControl 
        = new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);
}

control.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
        >
<ImageView
    android:id="@+id/img"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher"
    />
</LinearLayout>

如何从java代码中的 control.xml 中引用 ImageView?


斯蒂芬大帝
浏览 856回答 2
2回答

阿晨1998

View&nbsp;viewControl&nbsp;=&nbsp;controlInflater.inflate(R.layout.control,&nbsp;null); ImageView&nbsp;img&nbsp;=(ImageView)&nbsp;viewControl.findViewById(R.id.img);使用上面的代码来获取图像引用。
随时随地看视频慕课网APP

相关分类

Java
我要回答