当前位置:编程学习 > wap >>

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

下边这个是布局文件
<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,比如只有一行字那就只有一行的高度,这是为什么呢,一直没搞明白原因 inflate addView --------------------编程问答-------------------- 同问!我的问题和你很相似。
主布局是一个RelativeLayout,使用inflater+addview加入的LinearLayout属性值
android:layout_alignParentBottom = "true"也是无法起作用,但是将待加入的LinearLayout直接放到主布局中就显示正常 --------------------编程问答-------------------- 你addView的时候属性(第二个参数)是怎么设置的? --------------------编程问答--------------------
引用 2 楼 guoyoulei520 的回复:
你addView的时候属性(第二个参数)是怎么设置的?


似乎发现问题了,addView(View child)是我调用的函数类型,没有第二个参数。
而且我的inflate方法调用的也是inflate(int resource, ViewGroup root),第二个参数被我设为了null,没有第三个参数。
我刚刚试过将inflate的第二个参数设为主布局RelativeLayout,发现问题解决了,即待加入子布局的属性起作用了。
但是又面临一个新问题,我是通过不同的按钮来切换不同的布局,将不同的布局文件通过inflate+addview方法加入到主布局中。
如果我将所有的inflate方法的第二个参数都设为主布局的话,首先程序启动会直接显示所有的待加入布局,其次再按切换布局按钮就会报错。
我想应该要在按下切换按钮时,动态地设定父布局(我不知道对不对...)。不知道这个想法怎么实施,或者说有其他的解决办法。 --------------------编程问答-------------------- 问题解决了!
调用inflate (int resource, ViewGroup root, boolean attachToRoot) 方法。将第二个参数设为待加入布局的父布局,第三个参数为false
--------------------编程问答--------------------
引用 4 楼 ChangXiZhou 的回复:
问题解决了!
调用inflate (int resource, ViewGroup root, boolean attachToRoot) 方法。将第二个参数设为待加入布局的父布局,第三个参数为false
--------------------编程问答-------------------- 原来是这样的,学习了
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,