Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 对充气布局中视图的引用_Android_Android Layout_Android Inflate - Fatal编程技术网

Android 对充气布局中视图的引用

Android 对充气布局中视图的引用,android,android-layout,android-inflate,Android,Android Layout,Android Inflate,我有一个布局,其中包含4个具有以下id的图像视图:opp11、opp12、opp13、opp14。 我使用以下方法对此布局进行充气: public class FourOptions extends LinearLayout { public FourOptions(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater layoutInflater = (LayoutInfla

我有一个布局,其中包含4个具有以下id的图像视图:opp11、opp12、opp13、opp14。 我使用以下方法对此布局进行充气:

public class FourOptions extends LinearLayout {

public FourOptions(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater layoutInflater = (LayoutInflater)context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.par_fouroptions_layout, this);

}
我是这样做的:

fo = new FourOptions(this, null);
l.addView(fo);
    Root result = new Root(this, results[0], results[1], results[2], 45, -1, false); 
    //Root is a class that extends RelativeLayout
    RelativeLayout.LayoutParams paramsRoot = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    paramsRoot.addRule(RelativeLayout.ALIGN_LEFT, fo.findViewById(R.id.opp11).getId());
    paramsRoot.addRule(RelativeLayout.ALIGN_RIGHT, fo.findViewById(R.id.opp11).getId());
    paramsRoot.addRule(RelativeLayout.ALIGN_TOP, fo.findViewById(R.id.opp11).getId());
    paramsRoot.addRule(RelativeLayout.ALIGN_BOTTOM, fo.findViewById(R.id.opp11).getId());
    result.setLayoutParams(paramsRoot);
    l.addView(result);
l是对我添加上述布局的布局的引用。 这一切都很好。 我现在尝试向l添加另一个视图,并将此视图与opp11对齐。我是这样做的:

fo = new FourOptions(this, null);
l.addView(fo);
    Root result = new Root(this, results[0], results[1], results[2], 45, -1, false); 
    //Root is a class that extends RelativeLayout
    RelativeLayout.LayoutParams paramsRoot = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    paramsRoot.addRule(RelativeLayout.ALIGN_LEFT, fo.findViewById(R.id.opp11).getId());
    paramsRoot.addRule(RelativeLayout.ALIGN_RIGHT, fo.findViewById(R.id.opp11).getId());
    paramsRoot.addRule(RelativeLayout.ALIGN_TOP, fo.findViewById(R.id.opp11).getId());
    paramsRoot.addRule(RelativeLayout.ALIGN_BOTTOM, fo.findViewById(R.id.opp11).getId());
    result.setLayoutParams(paramsRoot);
    l.addView(result);
结果被添加到屏幕的左上角,就好像没有为其分配布局参数一样。 当我尝试将结果与主布局l中的视图对齐时,它会起作用。这让我相信问题在于 fo.findviewbyd(R.id.opp11.getId())

但我不确定。 我试过其他几种方法。都失败了。 谢谢你的建议

这是四个选项的布局:

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

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

     <ImageView
         android:id="@+id/opp11"
         android:contentDescription="@null"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/rectanglebuttons"
         android:gravity="center" />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

<ImageView
     android:id ="@+id/opp12"
     android:contentDescription="@null"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content" 
     android:gravity="center"
     android:src="@drawable/rectanglebuttons"
     />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

 </LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

    <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

<ImageView
     android:id ="@+id/opp13"
     android:contentDescription="@null"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content"
     android:gravity="center"
     android:src="@drawable/rectanglebuttons"
      />

 <View
    android:id="@+id/spaceview"
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

<ImageView
     android:id ="@+id/opp14"
     android:contentDescription="@null"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content"
     android:gravity="center"
     android:src="@drawable/rectanglebuttons"
      />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>
</LinearLayout>


请查看相关布局文档:

您描述的行为正是我阅读这些文档时所期望的。我想在这行输入:

RelativeLayout允许子视图指定它们相对于对象的位置 父视图或其他视图(由ID指定)。所以你可以把两个对齐 元素以右边框为中心,或使一个元素位于另一个元素之下,以 屏幕,居中左,等等。默认情况下,所有子视图都是 在布局的左上角绘制,因此必须定义位置 使用中提供的各种布局特性创建每个视图的 RelativeLayout.LayoutParams

不要将结果添加到
root
,而是将其添加到
fo
,您可能会看到所需的行为

请注意,这不是一个树生成描述,而是“相对于父视图或彼此”,这意味着您可以使用参数定位新视图:相对于父视图(
root
,在您的情况下)或其子视图(
of

编辑:您已经为fo布局添加了xml,我想我看到了问题:

您正在线性布局中包装op11和op12。除了上面关于相对布局的父子关系或子关系的陈述之外,这里还增加了一个问题

也就是说,您要做的是为xml中存在的包装器布局添加一个视图。您在该视图上没有id,但是有问题的代码块是:

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

     <ImageView
         android:id="@+id/opp11"
         android:contentDescription="@null"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/rectanglebuttons"
         android:gravity="center" />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

<ImageView
     android:id ="@+id/opp12"
     android:contentDescription="@null"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content" 
     android:gravity="center"
     android:src="@drawable/rectanglebuttons"
     />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

 </LinearLayout>


由于此线性布局是
fo
的子项,因此上述问题适用,但会出现第二个问题。即使您将视图直接添加到这个线性布局中,我认为您在尝试将RelativeLayout规则应用到线性布局时仍然会看到问题。将此视图更改为相对布局,进行相应修改,然后将结果视图添加到此包装器视图,您应该可以看到所需内容。

这可能是因为R.id.opp11位于另一个父级(FourOptions)内。您能否发布
par_FourOptions_layout.xml
I添加了par_FourOptions_layout.xml我已尝试将结果添加到fo中。通过fo.addView(result)和让fo的构造函数接收根对象并将其从类中放置。这两种方法都产生了奇怪的结果(par_fouroptions_layout.xml中的图像看起来更小,或者只有4个图像中的2个出现了…),因此我认为这与您的参数有关,它将尝试将您的视图与您引用的视图完美地对齐。它们可能是重叠的。而且这些结果一点也不奇怪——你正在向相同大小的视图添加更多内容。要么分配的空间需要增加,要么所有的东西都将缩小以适应同一个空间。它们不是坚果:)这是一种将两个对象集中在一起的方法,一个在垂直方向上,一个在水平方向上。但当我试图直接将结果添加到fo中时,我从未看到fo中的结果。图像大小更改,但结果不存在。。。