addView一个LinearLayout的布局显示有问题

chenmeng824 2013-04-09 02:54:33
下边这个是布局文件
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:fadingEdge="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/outerlayout"
android:orientation="vertical"
android:background="#374045"
android:paddingLeft="8dp"
android:paddingRight="8dp">


</LinearLayout>
</ScrollView>

下边这个是要添加到上边LinearLayout中的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="vertical">
<TextView android:id="@+id/singer_new_ttl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="15sp"
android:textColor="#e1e1e1"
android:singleLine="false"/>
<TextView
android:id="@+id/tx_fromanddate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#808080"
android:textSize="13sp"/>
</LinearLayout>

问题是这样的,如果把下边的布局直接放到上边的LinearLayout中显示正常,比如只有一行文字也会显示90dp的高度,但是如果通过inflate下边的布局然后再addView到上边LinearLayout中就不会是固定的90dp,比如只有一行字那就只有一行的高度,这是为什么呢,一直没搞明白原因
...全文
6160 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
别介意 2013-08-21
  • 打赏
  • 举报
回复
原来是这样的,学习了
凉凉二点凉 2013-08-19
  • 打赏
  • 举报
回复
引用 4 楼 ChangXiZhou 的回复:
问题解决了! 调用inflate (int resource, ViewGroup root, boolean attachToRoot) 方法。将第二个参数设为待加入布局的父布局,第三个参数为false
唱戏周 2013-08-19
  • 打赏
  • 举报
回复
问题解决了! 调用inflate (int resource, ViewGroup root, boolean attachToRoot) 方法。将第二个参数设为待加入布局的父布局,第三个参数为false
唱戏周 2013-08-19
  • 打赏
  • 举报
回复
引用 2 楼 guoyoulei520 的回复:
你addView的时候属性(第二个参数)是怎么设置的?
似乎发现问题了,addView(View child)是我调用的函数类型,没有第二个参数。 而且我的inflate方法调用的也是inflate(int resource, ViewGroup root),第二个参数被我设为了null,没有第三个参数。 我刚刚试过将inflate的第二个参数设为主布局RelativeLayout,发现问题解决了,即待加入子布局的属性起作用了。 但是又面临一个新问题,我是通过不同的按钮来切换不同的布局,将不同的布局文件通过inflate+addview方法加入到主布局中。 如果我将所有的inflate方法的第二个参数都设为主布局的话,首先程序启动会直接显示所有的待加入布局,其次再按切换布局按钮就会报错。 我想应该要在按下切换按钮时,动态地设定父布局(我不知道对不对...)。不知道这个想法怎么实施,或者说有其他的解决办法。
凉凉二点凉 2013-08-19
  • 打赏
  • 举报
回复
你addView的时候属性(第二个参数)是怎么设置的?
唱戏周 2013-08-19
  • 打赏
  • 举报
回复
同问!我的问题和你很相似。 主布局是一个RelativeLayout,使用inflater+addview加入的LinearLayout属性值 android:layout_alignParentBottom = "true"也是无法起作用,但是将待加入的LinearLayout直接放到主布局中就显示正常

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧