android - 自定义 View 未显示在水平 ScrollView 中不工作

标签 android android-custom-view horizontalscrollview

当我尝试将我的自定义 View 添加到水平 ScrollView 时,它没有显示。 但相同的自定义 View 显示在其他布局中,如相对和线性布局。

 layout=new LinearLayout(this);
        layout.setBackgroundColor(Color.YELLOW);
        layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
        layout.setOrientation(LinearLayout.HORIZONTAL);

        l = new PredicateLayout(this);
//       l.sendtext(info);
         for(int j=0;j<info.length;j++)
         {

            LinearLayout aa=new LinearLayout(this);
            aa.setOrientation(LinearLayout.VERTICAL);
            t = new TextView(this);
                t.setText(info[j]);
                t.setBackgroundColor(Color.GREEN);
                t.setTextSize(i);
                t.setWidth(screenwidth/2);
                aa.addView(t);
                l.addView(aa, new PredicateLayout.LayoutParams(0, 0));

         }

         layout.addView(l);
        scroll.addView(layout);

我的 XML 看起来像这样

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/scroll"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="horizontal" 
    >

</HorizontalScrollView>

也试过这样

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/scroll"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="horizontal" 
    >

    <com.test.app.PredicateLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:id="@+id/predicate_layout"
        >

    </com.omnion.bible.PredicateLayout> 
</HorizontalScrollView>

代码看起来像

test=(PredicateLayout)findViewById(R.id.predicate_layout);

 for(int j=0;j<info.length;j++)
         {

            LinearLayout aa=new LinearLayout(this);
            aa.setOrientation(LinearLayout.VERTICAL);
            t = new TextView(this);
                t.setText(info[j]);
                t.setBackgroundColor(Color.GREEN);
                t.setTextSize(i);
                t.setWidth(screenwidth/2);
                aa.addView(t);
                test.addView(aa, new PredicateLayout.LayoutParams(0, 0));

         }

谁能帮帮我。 提前致谢。

最佳答案

像这样放置你的自定义布局

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/scroll"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="horizontal" 
    >
<LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
    <com.test.app.PredicateLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:id="@+id/predicate_layout"
        >

    </com.omnion.bible.PredicateLayout> 
</LinearLayout>
</HorizontalScrollView>

关于android - 自定义 View 未显示在水平 ScrollView 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15338558/

相关文章:

android - 如何在Android中插入新的SQLite表行?

自定义 View 中的 Android 属性

android - 如何让 Android 中的 TextView 具有水平滚动和自动滚动?

android - 启动应用程序时,录制音频权限未显示在我的应用程序中

java - Android 上如何解析 JSON 数据?

android - 自定义 EditText 未在焦点上显示键盘

android - 在android中保存/恢复自定义 View 的状态

android - 如何获取 Horizo​​ntalScrollView 的宽度

android - 水平 ScrollView 的问题自动滚动到 Edittext 焦点

android - GPS 联系人位置