Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中动态创建UI_Android_Android Layout_Android Ui - Fatal编程技术网

在android中动态创建UI

在android中动态创建UI,android,android-layout,android-ui,Android,Android Layout,Android Ui,我想为平板电脑创建如图所示的UI。此UI将根据项目数重复。每个块(即小块或大块)的位置可以动态变化。请给我建议我应该使用哪个视图、布局。 任何建议都将不胜感激…提前感谢 这些“产品”是否总是能很好地符合您所画的线条?如果是这样,您可以使用相互嵌套的线性布局(垂直和水平)。这些“产品”是否总是能够很好地与您绘制的线条相吻合?如果这样,您可以使用彼此嵌套的线性布局(垂直和水平)。要为您应该使用的平板电脑设计此类UI,请在片段中添加/删除各自的布局。如果采用相对布局,则需要正确使用RelativeLa

我想为平板电脑创建如图所示的UI。此UI将根据项目数重复。每个块(即小块或大块)的位置可以动态变化。请给我建议我应该使用哪个视图、布局。 任何建议都将不胜感激…提前感谢


这些“产品”是否总是能很好地符合您所画的线条?如果是这样,您可以使用相互嵌套的线性布局(垂直和水平)。

这些“产品”是否总是能够很好地与您绘制的线条相吻合?如果这样,您可以使用彼此嵌套的线性布局(垂直和水平)。

要为您应该使用的平板电脑设计此类UI,请在片段中添加/删除各自的布局。如果采用相对布局,则需要正确使用RelativeLayout.LayoutParams

如果您正在寻找使用片段的最佳示例,请尝试源代码。它使用各种动态UI功能


希望有帮助

要为平板电脑设计这样的UI,您应该使用并在片段中添加/删除各自的布局。如果采用相对布局,则需要正确使用RelativeLayout.LayoutParams

如果您正在寻找使用片段的最佳示例,请尝试源代码。它使用各种动态UI功能


希望有帮助

如果您的目标是API v14或更高版本,则可以使用GridLayout:

如果您的目标是API v14或更高版本,则可以使用GridLayout:

在添加每个类别的位置使用LinearLayout,并对每个类别使用RelativeLayout是一个不错的选择。您应该根据包含的产品数量自定义相对布局的onlayout方法

您可以使用片段,也可以使用listview

图像更改后编辑:您只需扩展relativelayout并覆盖onLayout方法。这将比嵌套布局执行得更好,而且我想不出任何其他可以省力的方法

编辑以进行详细说明:

下面是我使用的一个示例类。基本上,在onlayout方法中,通过调用每个子视图的layout方法并传递矩形的参数,您可以做出每个决定并告诉所有子视图将它们放置在一个矩形中。了解如何使用子视图的布局方法来指定矩形

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); 
    }

}
}
public类KnockPile扩展了HandPile
{
私有静态最终字符串标记=“KnockPile”;
预期的静态最终int_卡=3;
int mColoring;//这是与此堆关联的着色组。
//每一张添加到这堆卡片上的卡片都应该设置这种颜色。
公共KnockPile(上下文上下文、GameActivity ref、int ingroup)
{
超级(上下文);
mColoring=ingroup;
mActivityReference=ref;
setWillNotDraw(假);
可点击设置(真);
setOnDragListener(本);
mCardBorders=newarraylist();
最终LayoutTransition转换=新的LayoutTransition();
setLayoutTransition(转换);
//transition.enableTransitionType(LayoutTransition.CHANGING);
//transition.disableTransitionType(LayoutTransition.CHANGE_出现);
}
/**
*这将覆盖handpile的卡片顺序。此方法以线性方式放置卡片。
*可能是重叠方式。这与屏幕方向无关。
*
*@see com.kavhe.kondi.gin.layouts.HandPile#onLayout(boolean,int,int,int,int,int)
*/
@凌驾
仅受保护的void布局(布尔值已更改,int l,int t,int r,int b){
int availablewidth=getMeasuredWidth();//这是可用的最大值
int distance=availablewidth/Math.max(getChildCount(),1);//两张卡之间的水平距离
Log.v(标签,“仅可用宽度布局为:“+availablewidth+”,距离为“+distance”);
int cardheath=getHeight();
int CARDWITH=卡片高度*3/4;
如果(距离>卡片宽度)距离=卡片宽度;
mCardBorders.clear();
对于(int i=0;i
这是从

仅限受保护的空心布局(布尔值已更改、整数左侧、整数顶部、整数右侧、整数底部)

当此视图应为其每个子视图指定大小和位置时,从布局调用。具有子级的派生类应重写此方法,并对其每个子级调用layout

参数

已更改这是此视图的新大小或位置

左-左位置,相对于父对象

顶部位置,相对于父级

相对于父对象的右位置


底部-底部位置,相对于父级,在添加每个类别的位置使用LinearLayout,并对每个类别使用RelativeLayout是一个不错的选择。您应该根据包含的产品数量自定义相对布局的onlayout方法

您可以使用片段,也可以使用listview

图像更改后编辑:您只需扩展relativelayout并覆盖onLayout方法。这将比嵌套布局执行得更好,而且我想不出任何其他可以省力的方法

编辑以进行详细说明:

下面是我使用的一个示例类。基本上,在onlayout方法中,通过调用每个子视图的layout方法并传递矩形的参数,您可以做出每个决定并告诉所有子视图将它们放置在一个矩形中。了解如何使用子视图的布局方法来指定矩形

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); 
    }

}
}
public类KnockPile扩展了HandPile
{
私有静态最终字符串标记=“KnockPile”;
预期的静态最终int_卡=3;
int mColoring;//这是与此堆关联的着色组。
//每一张添加到这堆卡片上的卡片都应该设置这种颜色。
公共敲门堆(上下文,GameA