gpt4 book ai didi

android - 动态添加ImageView到LinearLayout出错

转载 作者:行者123 更新时间:2023-11-29 20:56:30 25 4
gpt4 key购买 nike

我写了一个小函数

private void addTabIndicators(int tabCount){
LinearLayout indicatorsContainer = (LinearLayout)findViewById(R.id.indicators_container);
for(int i = 0; i<tabCount; i++){
ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, null);
indicatorsContainer.addView(indicator);
}
}

这应该根据寻呼机适配器中的选项卡数量向我的 Activity 中的线性布局添加圆圈。一切都会很酷但是,我添加的 imageviews 而不是 beeing 在 xml 布局中声明的大小,正在调整为 1x1px ......有什么我可能出错的想法吗?以下是指示器和线性布局的布局

tab_indicator.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="8dp"
android:src="@drawable/floating_button_background"/>

指标容器:

<LinearLayout
android:layout_marginBottom="16dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/indicators_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>

最佳答案

问题可能出在这里

    ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, null);

您需要传递 ImageView 的 Root View 以提供在 XML 中定义的布局。如果您改为传递 null,则会设置默认布局参数。

地点

    ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, your root view of image view,false);

这是一个很常见的错误。除非您真的知道自己在做什么,否则永远不要传递 null。

在这里阅读更多:

http://developer.android.com/reference/android/view/LayoutInflater.html

希望对你有帮助

关于android - 动态添加ImageView到LinearLayout出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27542344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com