繁体   English   中英

如何通过java将图像放置在Layout上? Android的

[英]How to place image at Layout by java? Android

我在drawable-mdpi文件夹中有一个名为c1.png的图像。 我希望用Java将它放到布局中:

package ...;

import android.os.Bundle;
//import android.provider.MediaStore.Images;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout layout = (LinearLayout)findViewById(getCurrentFocus().getId() );

        ImageView img = new ImageView(this);
        ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        img.setLayoutParams(lp);
        img.setImageDrawable(getResources().getDrawable(R.drawable.c1));
        layout.addView(img);
        setContentView(layout);//Doesn't matter, if it's commented or not. The error still exists.
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

我得到了:“不幸的是,%application_name已经停止了”。 我看到很多例子,在id params中使用R.id. *,但它也停止了(如R.id.linearLayout1),或者R没有这样的constansts(如R.id.c1)。 我怎样才能做到这一点?

你不需要做一个setContentView(布局); 另外,替换:

findViewById(getCurrentFocus().getId() )

通过

findViewById(R.id.layoutId); //layoutId is the id of the linearLayout defined in your layout.xml file. And R would refer to your local resources folder path and not the android Resources path.

你的xml文件可能看起来像这样(这将是main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    <LinearLayout
        android:id="@+id/layoutId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout layout = (LinearLayout)findViewById(R.id.layoutID ); // //layoutID is id of the linearLayout that defined in your main.xml file

    ImageView img = new ImageView(this);
    ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    img.setLayoutParams(lp);
    img.setBackgroundResource(R.drawable.c1);
    layout.addView(img);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

希望这可以帮助。

使用代码:

 ImageView img = new ImageView(this);
 img.setImageResource(R.drawable.c1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM