我的自定义 View 组类存在布局问题。
我在构造函数中调用了 init()。在我的 init() 函数中,我从一个 xml 文件扩展 View 组布局,该文件包含一个 LinearLayout,我向其中添加了几个其他 View 。
我正在使用 spacer (View layout_width="0"and layout_weight="1") 来平均分配项目。
问题就在这里:当我添加 child 时,布局仍然没有定义任何宽度。所以垫片的大小都是 0,实际上不会平均放置“line_dot”项目。我怎样才能以某种方式更新它们的大小?
public class SliderSwitch extends FrameLayout {
...
public SliderSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.slider_switch,this);
sliderLayout = (LinearLayout) findViewById(R.id.sliderLayout);
labelLayout = (LinearLayout) findViewById(R.id.labelLayout);
// add one spacer
View spacer = (View) inflate(context,R.layout.spacer, null);
sliderLayout.addView(spacer);
// setup the view depending on how many elements there are
for (int i=1;i<numberOfElements-1;i++) {
ImageView lineDot = (ImageView) inflater.inflate(R.layout.slider_switch_line_dot, null);
sliderLayout.addView(lineDot);
View spacer = (View) inflate(context,R.layout.spacer, null);
sliderLayout.addView(spacer);
}
}
...
}
这是垫片的 xml 文件:
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
这是我的 View 组布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/left_bg" />
<LinearLayout
android:id="@+id/sliderLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/middle_bg"
android:gravity="center"
android:orientation="horizontal" >
<!-- <include layout="@layout/spacer"/> -->
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/right_bg" />
</LinearLayout>
<ImageButton
android:id="@+id/sliderKnob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:background="@null"
android:src="@drawable/knob" />
</RelativeLayout>
</LinearLayout>
最佳答案
我现在找到了解决方案。您必须再次在代码中设置 LayoutParams(即使它们已经在 xml 文件中定义),然后一切都整齐地间隔开,这是应该的。似乎是一个错误。但是这个解决方案有效:
// add one spacer
View spacer = (View) inflate(context,R.layout.spacer, null);
spacer.setLayoutParams(new LinearLayout.LayoutParams(1,LayoutParams.WRAP_CONTENT,1.0F));
sliderLayout.addView(spacer);
// setup the view depending on how many elements there are
for (int i=1;i<numberOfElements-1;i++) {
ImageView lineDot = (ImageView) inflater.inflate(R.layout.slider_switch_line_dot, null);
sliderLayout.addView(lineDot);
spacer = (View) inflate(context,R.layout.spacer, null);
spacer.setLayoutParams(new LinearLayout.LayoutParams(1,LayoutParams.WRAP_CONTENT,1.0F));
sliderLayout.addView(spacer);
}
关于Android:将从 XML 布局膨胀的 View 添加到尚未定义任何宽度的 View 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13734824/