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

In my application, I have one (and only one) user interface element that is not specified in the XML layout file.

This element is a button created and returned at runtime by a third-party library (i.e. I have no control over this).

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

Is it possible to “reserve an empty slot” in the XML layout file for this button so 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 runtime with RelativeLayout.LayoutParams.addRule(), what would be the identifier of this dynamically created button, if it doesn't have a link in the XML layout file at all?

For example, with the following call:

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

What would I put instead R.id.btn_dynamically_created_button?

Refresh . Thanks to the question below, I created a place holder, for example:

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

Now the problem is this: how to associate the returned object with getDynamicallyCreatedButton () (the returned object is a subclass LinearLayout, not Button), with R.id.btn_dynamically_created_button?

EDIT: This thread seems to be dealing with a similar problem, but I'm not sure that I understand the proposed solution.

+3
source share
1 answer

:

  • LinearLayout /, , .
  • LinearLayout.
  • , LinearLayout.

, .

: :

( ): LinearLayout 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>

(, onCreate): ( , , , ), LinearLayout, LinearLayout.

b = getButton();//-

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);
+1

All Articles