Android View Not Appearing
I am attempting to have a new view appear on top of my main view.
Here is the XML for the new view:
<RelativeLayout
android:id="@+id/mapdetaillayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="This is the Text from the XML File."
android:id="@+id/DetailTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
And here is the code I am using to push the new viev onto the screen:
RelativeLayout DetailLayout = new RelativeLayout(c);
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.mapdetailview, DetailLayout);
TextView tv = (Te开发者_高级运维xtView) v.findViewById(R.id.DetailTextView);
tv.setText("HERE IS THE TEXT FROM THE CODE");
// This Log works
Log.i("MESSAGE", tv.getText());
DetailLayout.setBackgroundColor(Color.WHITE);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.NO_GRAVITY;
DetailLayout.bringToFront();
DetailLayout.setVisibility(View.VISIBLE);
The code is getting called and the Log outputs the expected text, which to me indicates that the View has been created - it just isn's being displayed on the screen.
Any help would be appreciated.
I do not see any call to Activity's setContentView() method in your code. Probably you just do not do that? If so, that's the reason why you do not see anything. Also you do not need to instantinate RelativeLayout manually. Try just to specify layout resource as content view:
setContentView(R.layout.mapdetailview);
You can then get your TextView just by:
TextView tv = (TextView) findViewById(R.id.DetailTextView);
I hope that helps.
Have you tried changing the LayoutParams.WRAP_CONTENT to fill_parent to see if anything changes?
精彩评论