【问题标题】:addView to an inflated LinearLayoutaddView 到膨胀的 LinearLayout
【发布时间】:2014-10-13 09:50:04
【问题描述】:

我正在尝试将Layout 添加到LinaerLayout。在第一次创建我的视图时,布局添加得很好,这意味着 inflate 和 addView 运行良好。但是当我再次单击一个按钮以执行该过程并再次添加相同的视图时,它可以工作,但该视图添加在第一个视图之上。

片段:

 // onCreateView, I bind the container with his LinearLayout
 mLnPricing = (ViewGroup) mRootView.findViewById(R.id.lnPricing);

 //some stuff

 private void createPriceBlock() {

    mParent = LayoutInflater.from(getActivity()).inflate(
            R.layout.block_pricing, null);
    //binding some views
    mIbAddPricing.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            createPriceBlock();
        }
    });
    mLnPricing.addView(mParent,0);
}

block_pricing.xml:

<?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="wrap_content"
android:orientation="vertical" >

//some text fields

</LinearLayout>

main_lauyou.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

//some controllers

    <LinearLayout
        android:id="@+id/lnPricing"
        style="@style/addLabels.AppTheme"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />




</ScrollView>

我不知道我的代码有什么问题,但出于某种奇怪的原因,相同的代码正在另一个项目中运行。

我的期望:在位置 0 添加新视图并将其他视图向下移动。

我所拥有的:视图添加在预览之上,并在视图下方创建了一个空白区域

【问题讨论】:

  • 最好选择CustomAdapterlistViewcustom view
  • mLnPricing.requestLayout();
  • @MikeM。谢谢你发现它,我真的错过了覆盖方向

标签: android android-view layout-inflater


【解决方案1】:

LinearLayout 的默认方向是水平的。您没有为新视图添加到的 LinearLayout 指定方向,因此旧视图被推到屏幕外右侧。将android:orientation="vertical" 添加到lnPricing LinearLayout 标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多