开发者

Android custom xml widget

开发者 https://www.devze.com 2022-12-27 01:47 出处:网络
I have an XML file in my layout folder that has how i want my custom widget/view (not sure what correct terminology is here).

I have an XML file in my layout folder that has how i want my custom widget/view (not sure what correct terminology is here).

but how do i make it so that i can programatically, add one or more to an activity

the xml file is as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/viewLog">
  <TextView android:id="@+id/viewLogClimbName"
            android:layout_width="fill_parent"
            android:singleLine="true"
            android:ellipsize="end"
            android:gravity="left"
  />
  <LinearLayout android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="w开发者_运维知识库rap_content"
                android:layout_weight="1">
    <LinearLayout android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content">
      <TextView android:id="@+id/viewLogDate"
                android:layout_width="fill_parent"
                android:singleLine="true"
                android:ellipsize="end"
                android:gravity="left"
      />
      <TextView android:id="@+id/viewLogStyle"
                android:layout_width="fill_parent"
                android:singleLine="true"
                android:ellipsize="end"
                android:gravity="left"
      />
    </LinearLayout>
    <TextView android:id="@+id/viewLogDetails"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:ellipsize="end"
              android:gravity="left"
              android:layout_weight="1"
    />
  </LinearLayout>
</LinearLayout>

I have looked at the android how-to and dont really understand what its getting at


LinearLayout root = (LinearLayout) findViewById(R.id.my_root_viewgroup);
View newView = View.inflate(this, R.layout.my_layout_file, null);
root.addView(newView);

You can cast newView if needed, and you can locate views within that inflated view using newView.findViewById(R.id.my_other_child_view)


Say your layout was named res/some_layout.xml

  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View newView = inflater.inflate(R.layout.some_layout, null);

Then add() your newView to some other view, say a LinearLayout from your main layout, retrieved with findViewById().

0

精彩评论

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