用于定制设计的Android嵌套线性布局

用于定制设计的Android嵌套线性布局,android,nested,Android,Nested,我想在活动的一个布局之后添加两个线性布局,每个布局的宽度都相同。问题是我无法通过编程设置这些布局的权重。我可以在xml中这样做,但我想在程序中这样做。以下是我想要的: 第一个按钮包含屏幕的全宽,但在此之后,两个按钮将共享相同的宽度,以此类推 显然,我们需要以编程方式来完成 我的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/ap

我想在活动的一个布局之后添加两个线性布局,每个布局的宽度都相同。问题是我无法通过编程设置这些布局的权重。我可以在xml中这样做,但我想在程序中这样做。以下是我想要的:

第一个按钮包含屏幕的全宽,但在此之后,两个按钮将共享相同的宽度,以此类推

显然,我们需要以编程方式来完成

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".HomeActivity">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </LinearLayout>


    </ScrollView>
</LinearLayout>
我得到如下输出:


请给出建议,哪种方法是最好的。

最简单的方法是使用带重量的线性布局。以下是一个基本示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:weightSum="3">


    <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:weightSum="2">
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:weightSum="2">
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

输出:


然后就可以开始实现图形了。在建议的布局中不需要滚动视图,但如果有必要,您当然可以在以后添加它。

最简单的方法是使用带权重的线性布局。以下是一个基本示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:weightSum="3">


    <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:weightSum="2">
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:weightSum="2">
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
        <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

输出:


然后就可以开始实现图形了。您不需要在建议的布局中使用滚动视图,但如果有必要,您当然可以稍后添加它。

答案如下。我尝过了。如果满足,则标记为回答:

try {

   DisplayMetrics dm = new DisplayMetrics();
   this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;   
    int j = 0 ;
    String image;
    for(int i=0;i<(**JsonArray**).length();i++) {

        JSONObject jsonObject1 = **JsonArray**.getJSONObject(i);


        if(i==0) {
            image = //DrawableImage;
            drawable = Drawable.createFromStream(getAssets().open(image), null);
            Button button = new Button(HomeActivity.this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 564);
            layoutParams.setMargins(60, 80, 60, 40);
            button.setBackground(drawable);
            button.setLayoutParams(layoutParams);
            linearLayout.addView(button);
        }
        else
        {
            if(i%2 == 1)
            {
               image = //DrawableImage;
                drawable = Drawable.createFromStream(getAssets().open(image), null);
                LinearLayout linearLayout1=new LinearLayout(this);
                linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
                Button button = new Button(HomeActivity.this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width/2-120, 564);
                layoutParams.setMargins(60, 80, 60, 40);
                button.setBackground(drawable);
                button.setLayoutParams(layoutParams);
                linearLayout1.addView(button);


                if(i+1 < data.length())
                {
                    JSONObject jsonObject2 = data.getJSONObject(i+1);
                    image = //DrawableImage;
                    drawable = Drawable.createFromStream(getAssets().open(image), null);
                    Button button2 = new Button(HomeActivity.this);
                    button2.setBackground(drawable);
                    button2.setLayoutParams(layoutParams);
                    linearLayout1.addView(button2);

                }

                linearLayout.addView(linearLayout1);

            }
            else
            {
                continue;
            }

        }


    }




}catch (IOException ex)
{
    return;
}
试试看{
DisplayMetrics dm=新的DisplayMetrics();
这个.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int j=0;
字符串图像;

对于(int i=0;i这是答案。我尝过了。如果满足,则标记为答案:

try {

   DisplayMetrics dm = new DisplayMetrics();
   this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;   
    int j = 0 ;
    String image;
    for(int i=0;i<(**JsonArray**).length();i++) {

        JSONObject jsonObject1 = **JsonArray**.getJSONObject(i);


        if(i==0) {
            image = //DrawableImage;
            drawable = Drawable.createFromStream(getAssets().open(image), null);
            Button button = new Button(HomeActivity.this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 564);
            layoutParams.setMargins(60, 80, 60, 40);
            button.setBackground(drawable);
            button.setLayoutParams(layoutParams);
            linearLayout.addView(button);
        }
        else
        {
            if(i%2 == 1)
            {
               image = //DrawableImage;
                drawable = Drawable.createFromStream(getAssets().open(image), null);
                LinearLayout linearLayout1=new LinearLayout(this);
                linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
                Button button = new Button(HomeActivity.this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width/2-120, 564);
                layoutParams.setMargins(60, 80, 60, 40);
                button.setBackground(drawable);
                button.setLayoutParams(layoutParams);
                linearLayout1.addView(button);


                if(i+1 < data.length())
                {
                    JSONObject jsonObject2 = data.getJSONObject(i+1);
                    image = //DrawableImage;
                    drawable = Drawable.createFromStream(getAssets().open(image), null);
                    Button button2 = new Button(HomeActivity.this);
                    button2.setBackground(drawable);
                    button2.setLayoutParams(layoutParams);
                    linearLayout1.addView(button2);

                }

                linearLayout.addView(linearLayout1);

            }
            else
            {
                continue;
            }

        }


    }




}catch (IOException ex)
{
    return;
}
试试看{
DisplayMetrics dm=新的DisplayMetrics();
这个.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int j=0;
字符串图像;

对于(int i=0;它有很多优点,但我需要编程。这就是问题所在。如果一个项目看起来像顶部,但多了一个项目,则会向其中添加一个小部分,依此类推。我尝试了,但没有更改。请给我详细的代码。检查以下答案:在这种情况下,他创建了3个垂直控件,但您的解决方案会有些类似。谢谢这里只有两个列。不同的是只有第一个按钮将包含全宽度。然后创建一个嵌套的线性布局并将这些参数应用到内部布局,而第一个按钮在外部布局中。非常感谢,但是我需要编程。这就是问题。如果一个项目看起来像是顶部,那么更多的是一个那么小的PAR。我试过了,但没有改变。请给我详细的代码。看看这个答案:在这种情况下,他正在创建3个垂直控件,但你的解决方案会有点类似。非常感谢。这里相同,只有两列。不同的是,第一个按钮将包含全宽。然后创建一个嵌套的线性布局,并应用这些p参数到内部布局,而第一个按钮在外部布局。非常感谢。它完成了我的查询。非常感谢。它完成了我的查询。