簡體   English   中英

在EditText上方對齊TextView

[英]Align TextView above EditText

我想將TextView對齊到EditText上方。 TextView不高於窗口的頂部邊緣 (紅線)和EditText (綠色線)之間的空間時,如以下屏幕截圖所示,以下代碼可以正常工作。

當TextView的行數超過屏幕截圖中的行數時,會發生問題:它只是“溢出”了EditText,但我想將EditText保留在前台。

換句話說:為了保持EditText的可見性,我想將TextView的底邊距放在綠線上,並使其向紅線方向增長。

在此處輸入圖片說明

// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);

// TEXTVIEW
// how to set bottom margins programmatically?
layout.addView(tv);

// EDITTEXT
// place the EditText to the bottom of the layout, working well
et.setGravity(Gravity.BOTTOM);
et.setInputType(InputType.TYPE_CLASS_TEXT);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.BOTTOM;
et.setLayoutParams(params);
layout.addView(et);

這應該為您做:

// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);

// TEXTVIEW
LinearLayout.LayoutParams paramstv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1f);
tv.setLayoutParams(paramstv);
layout.addView(tv);

// EDITTEXT
et.setGravity(Gravity.BOTTOM);
et.setInputType(InputType.TYPE_CLASS_TEXT);

LinearLayout.LayoutParams etparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,8f);
etparams.gravity = Gravity.BOTTOM;
et.setLayoutParams(etparams);
layout.addView(et);

我猜關鍵是將EditText和TextView都包裝到LayoutParams中。 可能您需要調整權重。

如果此方法不起作用,請嘗試使用XML文件創建布局(此操作更容易)。

嘗試使用相對布局,然后在父級底部設置edittext。。如果您不想切換到相對布局,請嘗試在文本視圖之前聲明編輯文本。

您可以將EditText類型的屬性設置為textMultiline

當您使用LinearLayout ,為視圖設置weight

LinearLayout.LayoutParams paramTextView = new LinearLayout.LayoutParams(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, 1.0f);
tv.setlayoutParams(paramTextView);

LinearLayout.LayoutParams paramEditText = new LinearLayout.LayoutParams(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, 3.0f);
et.setlayoutParams(paramEditText);

這將給您您期望的結果

下面的xml代碼可以解決您的問題...

<RelativeLayout 
android:layout_height="match_parent"
android:layout_width="match_parent">

<SrollView 
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_above="@+id/edittext_id"
android:layout_margin="7dp">

<TextView
        android:id="@+id/text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</SrollView>
<EditText 
android:id="@+id/edittext_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentBottom="true"/>

</RelativeLayout>

暫無
暫無

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

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