0

我正在尝试将线性布局添加到滚动视图这是我的代码编译的代码,但它没有向我显示新布局

这是原始布局(我想添加到它)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" 

android:layout_margin="15dp"
android:layout_marginTop="15dp">

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" 
     android:id="@+id/ViewHistoryImageLayout">

    <ImageView
        android:id="@+id/HistoryImage"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.76"
        android:gravity="center"
        android:padding="10dp"
        android:src="@drawable/upload" 
        android:contentDescription="@string/HistoryImage"/>

    <TextView
        android:id="@+id/TranslatedText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.12"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/translateImageButton" />

</LinearLayout>

这是我想多次添加的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/TranslationMenuLayout" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numStars="5" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

添加新布局的java代码是:

setContentView(R.layout.activity_view_history_image);
ScrollView sv = new ScrollView(this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);

代码编译良好,应用程序正在运行,但没有任何反应 .xml 文件之一有问题吗?

tnx

4

4 回答 4

1

您应该检索现有的 ScrollView 布局而不是创建它。

setContentView(R.layout.activity_view_history_image);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
ScrollView sv = (ScrollView) findViewById(R.id.ScrollView01);
sv.addView(ll);

可能有帮助。

于 2013-01-10T15:14:33.307 回答
0

我认为您需要将layout_width原始文件更改LinearLayoutwrap_content而不是match_parent. 它占据了整个布局

于 2013-01-10T15:11:57.197 回答
0

我有同样的问题。我试图在 onSizeChange 上执行此操作,但没有成功。所以这段代码帮助了我:

        layout.postDelayed(new Runnable() {
            @Override
            public void run() {

                LinearLayout ll = new LinearLayout(getContext());

                ll.setOrientation(LinearLayout.HORIZONTAL);

                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT);

                layout.addView(ll, params);
            }
        }, 100);

layout是ScrollView里面的LinearLayout。

于 2015-11-18T08:10:36.777 回答
0

我构建了不同的 ImageView 以使其正常工作,如下所示:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.selected_spotphotos_container);
        imageView = new ImageView(getApplicationContext());
        imageView1 = new ImageView(getApplicationContext());
        imageView2 = new ImageView(getApplicationContext());

        imageView.setImageBitmap(bmImg1);
        imageView1.setImageBitmap(bmImg2);
        imageView2.setImageBitmap(bmImg3);

        linearLayout.addView(imageView,0);
        linearLayout.addView(imageView1,1);
        linearLayout.addView(imageView2,2);

XML 代码是这样的:

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    >
....

<HorizontalScrollView
        android:id="@+id/hori_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false"
        android:foregroundGravity="left"
        android:paddingBottom="1dp"
        android:paddingTop="1dp"
        android:visibility="visible"
        >

        <LinearLayout
            android:id="@+id/selected_spotphotos_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:animateLayoutChanges="true"
            android:gravity="left"
            android:orientation="horizontal"
            android:visibility="visible"
            >

            <!-- images will be added dynamicall to this layout -->
        </LinearLayout>
    </HorizontalScrollView>
//here u can add different EditText or Button or whatever you want
</LinearLayout>

我希望这可以帮助某人。

于 2015-12-10T14:42:23.280 回答