简体   繁体   中英

Align TextView above EditText

I want to align a TextView above an EditText . The following code is working well when the TextView is not higher than the space between the Top Margin of the window (red line) and the EditText (green line), like in the screenshot.

The problem occurs when the TextView has more lines than in the screenshot: it just "overruns" the EditText, but I want to keep the EditText in foreground.

In other words: I would like to place the TextView's bottom margin onto the green line and let it grow towards the red line, in order to maintain the visibility of the EditText.

在此处输入图片说明

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

This should do it for you:

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

I guess the key is really to wrap both the EditText and TextView into LayoutParams. Probably you have to adjust the weights.

If this one doesn't work, try to create your layout with an XML file (the handling there is easier).

尝试使用相对布局,然后在父级底部设置edittext。。如果您不想切换到相对布局,请尝试在文本视图之前声明编辑文本。

您可以将EditText类型的属性设置为textMultiline

As you're using LinearLayout , set weight for your views

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

This shall give you the result you expect

This below xml code can solve your problem...

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM