0

在下面发布的代码中,我无法在单行中看到文本,并且一次只希望显示 6 个文本,但我可以看到 20 多个文本。你能看一下为什么会这样吗?

我需要我的观点是这样的,完整的观点应该被 6 个文本覆盖,只有 3 x 3

|阿法萨法法法斯法法斯法斯法| | | |cascsdfs asdaddasdada| | |

Mainactivity.java

package com.example.sampleoporj;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    Button mbutton;
    MainActivity activity;
    LinearLayout mEventsLayout;
    HorizontalScrollView mHorizontalscroll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mbutton = (Button)findViewById(R.id.button1);
        mEventsLayout = (LinearLayout)findViewById(R.id.eventslayout);


        mbutton.setOnClickListener(btnlistener);
        activity = this;


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    OnClickListener btnlistener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            for(int i = 0 ;i <10 ; i++)
            {

                LayoutInflater inflater = activity.getLayoutInflater();


                View view = (View)inflater.inflate(R.layout.samplelayout,null);

                mEventsLayout.addView(view);

            }
        }
    };

}

Activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/helloworld"
        android:text="@string/hello_world" />


    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/helloworld"
        android:id="@+id/horizontalscroll">

        <LinearLayout 

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/eventslayout"></LinearLayout>


    </HorizontalScrollView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="18dp"
        android:layout_toLeftOf="@+id/helloworld"
        android:text="Button" />

</RelativeLayout>

样本布局

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".45"
            android:orientation="vertical" >

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

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

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

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".10"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
             />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".45"
            android:orientation="vertical" >

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

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

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

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

我要求输出应该是这样的,我需要一个水平滚动并且一次只需要显示 3 + 3 个文本(在图像中我只显示了 2 + 2 )。我注意到如果我添加带有“fill_parent”参数的线性布局,水平滚动会缩小。

在此处输入图像描述

4

0 回答 0