添加一个TextView到的FrameLayout在一个确定的位置 [英] Add a Textview to a FrameLayout in a determined position
问题描述
我想添加一个的TextView
到的FrameLayout
。该的TextView
的 WRAP_CONTENT
的属性,所以它的增长当文字增长。我将它添加到的FrameLayout
具有这种功能:
I'm trying to add a textView
to a frameLayout
. The TextView
has wrap_content
properties, so it grows when the text grows. I'm adding it to a FrameLayout
with this function:
((FrameLayout) findViewById(R.id.mainLayout)).addView(mEditText);
这是框架布局的XML:
This is the xml of the frame layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
TextView的始终显示在屏幕的左上角。我想它在中间,或顶部中间定位。如何才能做到这一点?
我在<一看了看答案href="http://stackoverflow.com/questions/6535648/how-can-i-dynamically-set-the-position-of-view-in-android">How我可以动态设置的角度位置的Android?,他们也没有太大的帮助:(主要是造成死机。
The textView always appears in the top left corner of the screen. I'd like to position it in the middle, or top middle. How can this be done?
I looked over the answers in How can I dynamically set the position of view in Android? and they weren't much help :( Mainly caused crashes..
推荐答案
试试这个:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.FILLPARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
((FrameLayout) findViewById(R.id.mainLayout)).addView(mEditText, params);
更改重力,以满足您的需求。
Change Gravity to suit your need.
顺便说一句,框架布局应使用 FILL_PARENT
的宽度和高度
BTW, the frame layout should use fill_parent
for width and height
这篇关于添加一个TextView到的FrameLayout在一个确定的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!