简体   繁体   中英

Creating UI dynamically in android

I want to make UI for tablet like shown in images.This UI will repeat according to number of items. And the position of each block (ie small or big block) can change dynamically. Please give me suggestion which view,layout i should use. Any suggestion would be appreciated...thanks in advance.

在此处输入图片说明

如果您以API v14或更高版本为目标,则可以使用GridLayout: http : //developer.android.com/reference/android/widget/GridLayout.html

Using LinearLayout where each category is added and using RelativeLayout for each category is a good option. You should customize onlayout method of the relative layout as a function of number of products it contains.

You can use fragments, listview as well.

EDIT after image change: You can just extend relativelayout and override onLayout method. This will perform better than nested layouts and I can't think of anything else you can get away with less effort.

EDIT for elaboration:

Here is an example class I use. Basically in the onlayout method you make every decision and tell all the child views to lay them in a rectangle by calling layout method of each of them and passing the parameters of the rectangle. See how I use layout method of child view to dictate the rectangle.

public class KnockPile extends  HandPile
{

private static final String TAG = "KnockPile";

static final int EXPECTED_CARDS = 3;

int mColoring; // this is the coloring group this pile is associated with.
// every card added to this pile should be set with this color.


public KnockPile(Context context, GameActivity ref , int ingroup)
{
    super(context );
    mColoring = ingroup;
    mActivityReference = ref;

    setWillNotDraw(false);
    setClickable(true);
    setOnDragListener(this);
    mCardBorders = new ArrayList<Integer>(); 

    final LayoutTransition transition  = new LayoutTransition();
    setLayoutTransition(transition );
    //transition .enableTransitionType(LayoutTransition.CHANGING);
    //transition .disableTransitionType(LayoutTransition.CHANGE_APPEARING);

}



/**
 * this overrides the card ordering of handpile. This method lays the cards in a linear fashion.
 * Possibly overlapping fashion. This is not dependent to orientation of screen.
 *
 * @see com.kavhe.kondi.gin.layouts.HandPile#onLayout(boolean, int, int, int, int)
 */
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {   


    int availablewidth = getMeasuredWidth() ;//this is the maximum availlable  
    int distance = availablewidth /Math.max(getChildCount(), 1) ; // the horizontal distance between two cards
    Log.v(TAG,"available width onlayout is :" + availablewidth +"and distance is  " + distance);
    int cardheight = getHeight();
    int cardwidth  = cardheight*3/4; 
    if(distance > cardwidth) distance = cardwidth;
    mCardBorders.clear();
    for(int i = 0 ; i < mCards.size() ; i++)
    {     
        //save this border into a global variable so that it can be used when one of the cards dragged
        //by user to show that user can insert to that location
        mCardBorders.add( i*distance);
        mCards.get(i).layout(  i*distance ,  0 , cardwidth+ i*distance ,   cardheight); 
    }

}
}

and this is from documentation

protected void onLayout (boolean changed, int left, int top, int right, int bottom)

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters

changed This is a new size or position for this view

left Left position, relative to parent

top Top position, relative to parent

right Right position, relative to parent

bottom Bottom position, relative to parent

也可以通过具有行跨度属性的表布局

Are the "products" always going to fit nicely on lines as you have drawn? If then, you can use LinearLayouts (Vertical and Horizontal) nested in each other.

To design such UIs for Tablets you should use Fragments and inside the fragment add/remove your respective layouts. If you are taking Relative Layouts then proper usage of RelativeLayout.LayoutParams is required.

If you are looking for a best example which uses Fragments try Google IO app source. It uses various dynamic UI features.

Hope it helps !

With the help of StaggeredGridView I sloved my requirement. Thanks to all for help and support.

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