Who can met what? When there is xml with layout, and I want to get the code that generates the same layout in runtime.
- An interesting question ... I don’t need it myself, but if I haven’t done it yet, I would undertake to write ... - Vladyslav Matviienko
- Not so long ago I saw a couple of corresponding plugins in the standard IntelliJ IDEA repositories. In the form of a standalone utility, I did not see it, however, and did not look for it. In general, I can not imagine who and why this may be needed. Do not share the use-case? - falstaf
- @falstaf, look. We need some kind of custom view (inside it will be a TextView, Button ...), I’m hiding the whole processing logic inside its view, just properties. It is possible inside our custom to make inflate from layout, this layout lies in resources. But, first, it is not always desirable that such minor controls clutter up res \ layout. And, secondly, for good in this layout, you need to do the root view <merge>, because if you just set LinearLayout, and our custom view will also be inherited from LinearLayout, then we get one extra container. - Yura Shinkarev
- oneI am waiting for the question "as the code that creates the layout turn into xml"! - KoVadim
- one@KoVadim: 1) run the code, 2) serialize the generated visual tree in XML: - / - VladD
|
1 answer
And, secondly, for good in this layout, you need to do the root view <merge>, because if you just set LinearLayout, and our custom view will also be inherited from LinearLayout, then we get one extra container.
You are wrong, you can do it without much investment. There are wonderful tricks. The principle is similar to ViewHolder.
We take xml markup
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" > <!--icon--> <ImageView android:id="@+id/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="0" /> <!--names--> <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:layout_weight="1"> <TextView android:id="@+id/name1" android:layout_height="wrap_content" android:layout_width="match_parent" /> <TextView android:id="@+id/name2" android:layout_height="wrap_content" android:layout_width="match_parent" android:paddingRight="5dp" /> </LinearLayout> </LinearLayout> And create a class that will act as a constructor.
public class MyCustomHolder { private final View mainContent; private final ImageView icon; private final TextView textView; private final TextView textView2; private CustomObject object; private final Context mContext; public MyCustomHolder(final Context context) { mContext = context; mainContent = LayoutInflater.from(context).inflate(R.layout.your_layout, null); icon = ((ImageView) mainContent.findViewById(R.id.icon)); textView = (TextView) mainContent.findViewById(R.id.name1); textView2 = (TextView) mainContent.findViewById(R.id.name2); mainContent.setTag(this); } public void setObject(final CustomObject object) { this.object = object; textView.setText(object.getName()); textView2.setText(object.getSubtitle()); icon.setImageResource(R.drawable.icon); } public View getView() { return mainContent; } public CustomObject getObject() { return object; } } It becomes quite easy to use.
MyCustomHolder holder = new MyCustomHolder(context); holder.setObject(myObject); //наполняем данными View myView = holder.getView();//получаем нужный View ((MyCustomHolder)myView.getTag())//достаем holder для изменения View This technique is much more convenient than generating a huge canvas of code. In addition, not everything can be done easily in code, which allows markup in xml.
- one> not everything can be done in the code, which allows markup in xml for example? - falstaf
- @falstaf, probably meant "not everything can be easily done in the code." - Helisia
- @SuperCreeper yes, exactly. corrected - andreich
|