If I were on a view view.setBackgroundResource (R.draw.ic);
Now I want to get my id (int) on this R.draw.ic view settings, and how to operate?<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
String resTypeName = getResources () getResourceTypeName (R.id. controls);.
String resEntryName = getResources () getResourceEntryName (R.id. controls);.
Context apk = createPackageContext (packageName,
Context.CONTEXT_IGNORE_SECURITY);
int drawavleId = apk.getResources (). getIdentifier (resEntryName, resTypeName,
apk.getPackageName ());
Reply:
Why should get this id, ah, this picture needed elsewhere, directly R.draw.ic to call does not have to
Reply:
Because my view is dynamically generated images in several randomly selected a picture background R.drawable. *
* In fact, I do not know what
So I want to know what can be later
Reply:
.
Said did not understand
This is my code:
package com.example.popstar;
import java.util.Random;
import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
private TableLayout tableLayout;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
tableLayout = (TableLayout) findViewById (R.id.tablelayout);
for (int row = 0; row <10; row + +) {
TableRow tableRow = new TableRow (this);
for (int col = 0; col <10; col + +) {
TextView textView = this.new_textview (row, col);
tableRow.addView (textView);
}
tableLayout.addView (tableRow);
}
}
/ / Create TextView
public TextView new_textview (int row, int col) {
TextView textView = new TextView (this);
textView.setId (row * 10 + col +1);
textView.setTextAppearance (this, R.style.textview);
textView.setLayoutParams (new TableRow.LayoutParams (45, 45));
int random = getRandom (5);
if (random == 0) {
textView.setBackgroundResource (R.drawable.red);
} Else if (random == 1) {
textView.setBackgroundResource (R.drawable.blue);
} Else if (random == 2) {
textView.setBackgroundResource (R.drawable.green);
} Else if (random == 3) {
textView.setBackgroundResource (R.drawable.zi);
} Else if (random == 4) {
textView.setBackgroundResource (R.drawable.yellow);
}
textViewOnClick (textView);
return textView;
}
/ / Generate a random number
public static int getRandom (int max) {
return new Random () nextInt (max);.
}
/ / TextView mouse click event
public void textViewOnClick (TextView textView) {
textView.setOnClickListener (new TextView.OnClickListener () {
public void onClick (View v) {
/ / I would like to get a background image
I click on the view to his set}
});
}
}
Reply:
private int drawable_id;
if (random == 0) {
drawable_id = R.drawable.red;
} Else if (random == 1) {
drawable_id = R.drawable.blue;
} Else if (random == 2) {
drawable_id = R.drawable.green;
} Else if (random == 3) {
drawable_id = R.drawable.zi;
} Else if (random == 4) {
drawable_id = R.drawable.yellow;
}
textView.setBackgroundResource (drawable_id); / / textView mouse click event
public void textViewOnClick (TextView textView) {
textView.setOnClickListener (new TextView.OnClickListener () {
public void onClick (View v) {
/ / View is drawable_id the background.
}
});
}
That's it.
Reply:
Reply:
This is not the result I wanted
Set of global variables can only get the last view of the picture id
According to fetch the background image information
id when the situation seems to me only after the 100 view dynamically generated is stored in a single file and set the background image id and picture information needsI do not know this idea right
No comments:
Post a Comment