【问题标题】:ScrollView Not working its just showing empty viewScrollView 不工作它只是显示空视图
【发布时间】:2014-03-27 10:00:16
【问题描述】:

我正在尝试在滚动视图中添加一个文本视图,然后以编程方式将此滚动视图添加到相对布局,但它只是显示空视图,即使只将文本视图添加到相对布局它的工作。

    TextView aboutTextView = new TextView(this);
    aboutTextView.setText(R.string.aboutText);

    aboutTextView.setId(5);
    aboutTextView.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    ScrollView scroll = new ScrollView(this);
    scroll.setPadding(R.dimen.scroll_padding, R.dimen.scroll_padding,
            R.dimen.scroll_padding, R.dimen.scroll_padding);
    scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    scroll.addView(aboutTextView);

    RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
    fragmentContainer.addView(scroll);

【问题讨论】:

    标签: android android-relativelayout textview android-scrollview


    【解决方案1】:

    试试这个代码。我希望它会有用。

    1) Java 代码

    package com.temp;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.RelativeLayout;
    import android.widget.ScrollView;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        TextView aboutTextView = new TextView(this);
        aboutTextView
                .setText("Temp Text Temp Text Temp");
    
        aboutTextView.setId(5);
        aboutTextView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    
        ScrollView scroll = new ScrollView(this);
        scroll.setPadding(5, 5, 5, 5);
        scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        scroll.addView(aboutTextView);
    
        RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
        fragmentContainer.addView(scroll);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    }
    

    2) 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" >
    
        <RelativeLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        </RelativeLayout>
    
    </RelativeLayout>
    

    Temp Text Temp Text Temp 的地方放你的文字。
    它对我有用。看看吧。

    谢谢。

    【讨论】:

    • 是的,它的工作感谢问题在于我设置它的方式。
    【解决方案2】:
    ScrollView scroll = new ScrollView(context);
    scroll.setBackgroundColor(android.R.color.transparent);
    scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                                 LayoutParams.FILL_PARENT));
    scroll.addView(yourview);
    

    【讨论】:

    • 感谢您的快速回复,我已经在 LinearLayout 中添加了 textview,然后将其添加到 scrollview 还设置了滚动视图的 BackgroundColor 但它仍然无法正常工作。
    • 你需要可滚动的文本视图吗?
    • @runtime 还是要在 xml 中?
    • android:maxLines = "AN_INTEGER" android:scrollbars = "vertical" 然后使用:textView.setMovementMethod(new ScrollingMovementMethod());
    【解决方案3】:

    不要在获取RelativeLayout 对象的同一语句中添加ScrollView。 而且我认为您应该指定您正在使用的 LayoutParams 实现。

    aboutTextView.setLayoutParams(new ScrollView.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    ScrollView scroll = new ScrollView(this);
    int scrollPadding = getResources().getDimensionPixelSize(R.dimen.scroll_padding);
    scroll.setPadding(scrollPadding, scrollPadding, scrollPadding, scrollPadding);
    scroll.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
    scroll.addView(aboutTextView);
    RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
        fragmentContainer;
    fragmentContainer.addView(scroll);
    

    【讨论】:

    • 如果你想在dimens.xml中定义它,你必须得到填充值getDimensionPixelSize。像这样:getResources().getDimensionPixelSize(R.dimen.scroll_padding);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    相关资源
    最近更新 更多