简体   繁体   中英

convert android UI xml to code

So, I'm trying to make a dynamic UI, and i want to add a seperator to it. unfortunately, i could only find out how do one in XML. is it possible to turn this

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/seperator"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="2dp"
    android:scaleType="fitXY"
    android:src="@android:drawable/divider_horizontal_dark" />

into program code?

my best attempt was

ImageView seperator=new ImageView(this);
seperator.setImageDrawable(drawable.divider_horizontal_dark);

Put it in an extra layout file and inflate it when you need it in code - I think that what you want to do and should be the easiest way.

In your Activity:

View v = LayoutInflater.from(this).inflate(R.layout.seperator, null);

If you inflate a Layout:

LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_layout, null);
TextView tv = (TextView) ll.findViewById(R.id.tv);

There is a website can convert that for you. You can design the interface with eclipse then submit the generated xml to XMLtoJAVA online converter and it should do it for you..

You can also create a View, define a background and add it with a LayoutParams

    ViewGroup container = (ViewGroup) findViewById(R.id.container); 
    View separator = new View(context);
    separator.setBackgroundColor(Color.Black);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 2);

    container.addView(separator, layoutParams);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM