开发者

Reserve a "slot" in a layout XML file for a dynamically created button?

In my app, I have one (and only one) UI element which isn't referenced in the XML layout file.

That element is a button, instantiated and returned at run-time by a 3rd party library (i.e. I don't have control over that).

My problem is that I would like some of the elements (TextViews) in the XML layout file to be placed relative to that button, using RelativeLayout.

Is it possible to "reserve an empty slot" in the XML layout file for that button such that I can do something like the following?

    <TextView android:id="@+id/tv_text_under_button"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_dynamically_created_button"
        android:text="" />

Alternatively, if I were to set the layout at run-time using RelativeLayout.LayoutParams.addRule(), what would be the ID of that dynamically created button, if it has no reference at all in the XML layout file?

开发者_Python百科

For example, in the following call:

layoutParams.addRule(RelativeLayout.BELOW, R.id.btn_dynamically_created_button);

What would I put instead of R.id.btn_dynamically_created_button?

Update: Thanks to the answer below, I created a place holder like this:

    <LinearLayout android:id="@+id/btn_dynamically_created_button"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    </LinearLayout>

The challenge now is: How to associate the returned object from getDynamicallyCreatedButton() (returned object is subclass of LinearLayout, not Button), with R.id.btn_dynamically_created_button?

EDIT: This thread seem to address a similar issue, but I am not sure that I understand the solution offered.


I'd suggest:

  1. Put a LinearLayout with width/height set to wrap-content, horizontal orientation and zero padding as the placeholder.
  2. Orient all the other things to that LinearLayout.
  3. When its time to put the button, simply stick it into the LinearLayout.

See if that works for you.

EDIT: attempt at a short example:

The layout (suitably shortened): you can place other components relative to the LinearLayout with id LinearLayout01.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_marginTop="2sp" android:layout_marginBottom="2sp" android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:gravity="right" style="@style/SimpleButtonBar" android:layout_below="@+id/rootlayout" android:layout_alignParentBottom="true">
        </LinearLayout>
        <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_above="@+id/LinearLayout01" android:fillViewport="true">
                <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/detaillayout">
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

The code (for example, this would go in onCreate): fetch your button (you need to make sure it has the right Context, but I figure you're doing that alright), fetch the LinearLayout, create a layout parameters object and stick your button into the LinearLayout.

Button b = getButton(); // retrieve your button somehow

LinearLayout l = (LinearLayout)findViewById(R.id.LinearLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
l.addView(b, lp);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜