簡體   English   中英

如何以編程方式將TextView和EditText對齊到水平LinearLayout中?

[英]How to align the TextView and EditText into horizontal LinearLayout programmatically?

這是我所擁有的:

LinearLayout horizontalLL = new LinearLayout(thisActivity);
horizontalLL.setOrientation(LinearLayout.HORIZONTAL);
horizontalLL.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT,1));

TextView captionTV = new TextView(thisActivity);
captionTV.setText(element.description);

captionTV.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT));
editText.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT,0.6f));

horizontalLL.addView(captionTV);
horizontalLL.addView(editText);

LL.addView(horizontalLL);

但是結果很糟糕:editText仍然很短,無法填充水平線性布局。 我也嘗試為captionTV設置權重,但仍然沒有結果。

請按以下方式進行更改

editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,0.6f));

您可以在此處使用LinearLayout.LayoutParams.MATCH_PARENTLinearLayout.LayoutParams.WRAP_CONTENT代替0

如果可能,將所有LinearLayoutCompat替換為LinearLayout

如果您希望您的EditText自動覆蓋剩余空間 ,請嘗試以下代碼:

LinearLayout horizontalLL = new LinearLayout(this);
        horizontalLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        horizontalLL.setOrientation(LinearLayout.HORIZONTAL);


        TextView captionTV = new TextView(this);
        captionTV.setText("element.description");

        captionTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT , 1));

        horizontalLL.addView(captionTV);
        horizontalLL.addView(editText);

        LL.addView(horizontalLL);

您需要使用以下代碼:

 LinearLayout ll1 = (LinearLayout)findViewById(R.id.ll1);

        android.widget.LinearLayout horizontalLL = new android.widget.LinearLayout(this);
        horizontalLL.setOrientation(android.widget.LinearLayout.HORIZONTAL);
        horizontalLL.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, 150,1f));

        TextView captionTV = new TextView(this);
        captionTV.setText("Hello textview ");
        captionTV.setTextColor(Color.BLUE);
        captionTV.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,0.4f));
        horizontalLL.addView(captionTV);

        EditText editText = new EditText(this);
        editText.setText("Hello edittext ");
        editText.setTextColor(Color.BLUE);
        editText.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,0.6f));

        horizontalLL.addView(editText);
        ll1.addView(horizontalLL);

因為在XML權重中定義如下:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1.0">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content" 
                android:layout_weight="0.40"
                android:text="test"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.60"
                android:text="test"/>
        </LinearLayout> 

暫無
暫無

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

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