2013-03-06 3 views
0

나는 drawable-mdpi 폴더에 c1.png라는 이미지가 있습니다. 나는이 자바로 레이아웃에 넣어하려면 :Java로 Layout에 이미지를 배치하는 방법은 무엇입니까? Android

내가 가진
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; 
    } 


} 

: 그것을 시작 때 "불행하게도 %의 응용 프로그램 이름이 중지되었습니다." id 매개 변수에 R.id. *를 사용하여 많은 예제를 보았지만 R.id.linearLayout1과 같은 R.id. *를 사용하거나 R이 같은 제약을 갖지 않았습니다 (예 : R.id.c1). 어떻게해야합니까?

답변

1
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; 
} 
} 

희망이 도움이 .

1

setContentView (레이아웃)를 수행 할 필요가 없습니다. 또한, 대체 :

findViewById(getCurrentFocus().getId()) 

에 의해 는 는 이 같은 (이 될 것 main.xml에) 볼 수 있었다

당신 XML 파일

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 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> 
+0

는 선형 – user2136963

+1

주요하지 RelativeLayout의있을 것입니다 그리고 그 안에 당신은 ID가있는 LinearLayout있을 것입니다. 편집 된 ans를 확인하십시오. – lokoko

+0

글쎄, 그것은 효과가 있었지만 PAD 코드 만 사용했다. 저는 다른 방식으로 이미지 작업을해야했습니다. 고마워요! – user2136963

0

를 사용하여 코드 : 사실, 내 레이아웃이 RelativeLayout의 보이는 경우

ImageView img = new ImageView(this); 
img.setImageResource(R.drawable.c1); 
관련 문제