按下按钮时的Android Auto Scroll

时间:2016-10-03 10:24:18

标签: android xml android-layout

我有一个XML布局,可以在屏幕底部放置一个按钮。我想按下这个按钮,在按下时显示一个文本,带有一种自动滚动。 我真的不知道该怎么做。有什么建议吗?

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.thefe.newsmartkedex.MainActivity">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/pkmn"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/tmpPkmn" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/tipo"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/tmpPkmn"
        android:layout_marginStart="19dp"
        android:layout_marginTop="89dp"
        android:id="@+id/tipo1" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/tipo"
        android:id="@+id/tipo2"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/tipo1"
        android:layout_alignParentEnd="true"
        android:layout_alignStart="@+id/tipo1" />

    <TextView
        android:text="Tipo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tipo"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="36dp"
        android:textSize="20dp"
        android:textAlignment="center"
        android:layout_alignStart="@+id/tipo1" />

    <TextView
        android:text="Forte contro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:textSize="20dp"
        android:id="@+id/forteContro" />

    <TextView
        android:text="Debole contro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/forteContro"
        android:layout_marginTop="108dp"
        android:textSize="20dp"
        android:id="@+id/deboleContro" />

    <!--tsf = tiposmallforte-->
    <ImageView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/forteContro"
        android:id="@+id/tsf1"
        android:src="@drawable/tipoSmall"/>

    <ImageView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/forteContro"
        android:layout_toRightOf="@id/tsf1"
        android:id="@+id/tsf2"
        android:src="@drawable/tipoSmall"/>

    <ImageView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/forteContro"
        android:layout_toRightOf="@id/tsf2"
        android:id="@+id/tsf3"
        android:src="@drawable/tipoSmall"/>

    <ImageView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/forteContro"
        android:layout_toRightOf="@id/tsf3"
        android:id="@+id/tsf4"
        android:src="@drawable/tipoSmall"/>

    <!--tsd = tiposmalldebole-->
    <ImageView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/deboleContro"
        android:id="@+id/tsd1"
        android:src="@drawable/tipoSmall"/>

    <ImageView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/deboleContro"
        android:layout_toRightOf="@id/tsd1"
        android:id="@+id/tsd2"
        android:src="@drawable/tipoSmall"/>

    <ImageView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/deboleContro"
        android:layout_toRightOf="@id/tsd2"
        android:id="@+id/tsd3"
        android:src="@drawable/tipoSmall"/>

    <ImageView
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/deboleContro"
        android:layout_toRightOf="@id/tsd3"
        android:id="@+id/tsd4"
        android:src="@drawable/tipoSmall"/>

    <Button
        android:text="Descrizione"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:id="@+id/descrizione" />


</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

首先将所有内容包装在 ScrollView 中 现在将您的按钮和textview放在LinearLayout中,如下所示

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true">

    <Button
        android:text="Descrizione"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/descrizione" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:visibility="gone" />

</LinearLayout>

现在,在按钮点击操作中,将文本设置为textView,并且不要忘记将其可见性更改为可见。最后使用 scrollView.fullScroll(View.FOCUS_DOWN)

滚动到textView

答案 1 :(得分:0)

-----对于Auto Scrolling webview,单击Toggle Button ----

public class MainActivity extends AppCompatActivity {
    TimerTask mTimerTask;
    final Handler handler = new Handler();
    Timer t = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ToggleButton toggleButton = (ToggleButton)findViewById(R.id.toggleButton);
        final WebView webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl("https://en.wikipedia.org/wiki/Blog");
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
                if (ischecked) {
                    Toast.makeText(MainActivity.this,"Start AutoScrolling", Toast.LENGTH_SHORT).show();
                  doTimerTask();   }
                else{

                    stopTask();
                    Toast.makeText(MainActivity.this,"Stop AutoScrolling", Toast.LENGTH_SHORT).show();
                }
            }
            private void stopTask() {
                mTimerTask.cancel();
        }
           private void doTimerTask() {
                mTimerTask = new TimerTask() {
                    public void run() {
                        handler.post(new Runnable() {
                            public void run() {
                                webView.post(new Runnable() {
                                    public void run() {
                                        if (webView.getContentHeight() * webView.getScale() >= webView.getScrollY()) {
                                            webView.scrollBy(0, 5);

                                        }
                                   }
                                });
                            }
                       });
                    }};
                // public void schedule (TimerTask task, long delay, long period)
                t.schedule(mTimerTask, 0, 500);
            }

        });
    }
}
相关问题