簡體   English   中英

在垂直線性布局內創建動態水平線性布局

[英]Create dynamic horizontal linearlayout inside a vertical linear layout

我在水平線性布局中有一些圓形圖像和textview。 我的水平線性布局位於垂直線性布局的內部。 我試圖在垂直布局中添加水平布局,但這給了致命的異常。我是android布局的新手,所以不知道下一步該怎么做。

Subcription.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscription);
        createpage();
    }
    public void  createpage()
    {
        LinearLayout LLV=(LinearLayout)findViewById(R.id.LLV);
        for(int i=0;i<6;i++){
            LinearLayout LLH=(LinearLayout)findViewById(R.id.LLH);
            LLH.setOrientation(LinearLayout.HORIZONTAL);
            CircleImageView circleImageView2=(CircleImageView)findViewById(R.id.circleimage2);
            TextView textView=(TextView)findViewById(R.id.textView4);
            //Log.d("idname",findViewById(R.id.circleimage2).toString());

            circleImageView2.setImageResource(R.drawable.test);
            textView.setText("DIPTO");
            LLH.addView(circleImageView2);
            LLH.addView(textView);
            LLV.addView(LLH);
        }

    }

activity_subscription.xml

<ScrollView
        android:layout_width="395dp"
        android:layout_height="543dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

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

            <LinearLayout
                android:id="@+id/LLH"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
                <de.hdodenhof.circleimageview.CircleImageView
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/circleimage2"
                    android:layout_width="96dp"
                    android:layout_height="96dp"
                    android:src="@mipmap/ic_launcher"
                    app:civ_border_width="4dp"
                    app:civ_border_color="#00000000"/>
                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="200dp"
                    android:layout_height="70dp"
                    android:layout_marginTop="8dp"
                    android:text="TextView" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

指定的孩子已經有一個父母。 您必須先在>子項的父項上調用removeView()。

LinearLayout LLH已存在於xml中並具有父項。 您要向LLV添加動態布局,然后替換它

LinearLayout LLH=(LinearLayout)findViewById(R.id.LLH);
        LLH.setOrientation(LinearLayout.HORIZONTAL);
        CircleImageView circleImageView2=(CircleImageView)findViewById(R.id.circleimage2);
        TextView textView=(TextView)findViewById(R.id.textView4);
        //Log.d("idname",findViewById(R.id.circleimage2).toString());

        circleImageView2.setImageResource(R.drawable.test);
        textView.setText("DIPTO");
        LLH.addView(circleImageView2);
        LLH.addView(textView);
        LLV.addView(LLH);

有了這個

LinearLayout LLH= new LinearLayout(this);
        LLH.setOrientation(LinearLayout.HORIZONTAL);
        CircleImageView circleImageView2=new CircleImageView(this);
        TextView textView=new TextView(this);
        //Log.d("idname",findViewById(R.id.circleimage2).toString());

        circleImageView2.setImageResource(R.drawable.test);
        textView.setText("DIPTO");
        LLH.addView(circleImageView2);
        LLH.addView(textView);
        LLV.addView(LLH);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM