android – 在确定的位置将Textview添加到FrameLayout

前端之家收集整理的这篇文章主要介绍了android – 在确定的位置将Textview添加到FrameLayout前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将textView添加到frameLayout. TextView具有wrap_content属性,因此在文本增长时会增长.我正在使用此函数将其添加到FrameLayout:
((FrameLayout) findViewById(R.id.mainLayout)).addView(mEditText);

这是框架布局的xml:

<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始终显示在屏幕的左上角.我想把它放在中间或中间位置.如何才能做到这一点?
我查看了How can I dynamically set the position of view in Android?的答案,他们没有多大帮助:(主要是造成崩溃..

解决方法

试试这个:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,Gravity.CENTER);

((FrameLayout) findViewById(R.id.mainLayout)).addView(mEditText,params);

根据您的需要更改Gravity.

顺便说一下,框架布局应该使用fill_parent来表示宽度和高度

原文链接:https://www.f2er.com/android/317181.html

猜你在找的Android相关文章