Сат ответ правильный, т. Е. Вам нужен правильный макет, но setContentView()
- не единственный способ ссылки на представление . Вы можете использовать LayoutInflater
для надувания родительского макета View
(даже если он не показан) и использовать этот недавно накаченный макет для ссылки на представление.
public class MyActivity extends Activity {
Context context;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
// the rest of yout code
private void someCallback() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.iconLayout, null);
ImageView img = (ImageView) ll.findViewById(R.id.application_icon);
// you can do whatever you need with the img (get the Bitmap, maybe?)
Единственное изменение, которое вам необходимо внести в ваш xml-файл, это добавить идентификатор в родительский макет, чтобы вы могли использовать его с LayoutInflater
:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="iconLayout"
<!-- all the rest of your layout -->
<ImageView android:id="@+id/application_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<!-- all the rest of your layout -->
</LinearLayout>