2016-10-03 160 views
0

我有一个XML布局,在屏幕底部放置一个按钮。我想在这个按钮下面显示一个文本,当它被按下时,用一种自动滚动。 我真的不知道该怎么做。任何建议?Android按钮自动滚动

我的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> 
+0

如果要滚动到你可以做的特定视图:'view.requestFocus()'。如果你有一个列表(使用recyclerview),你可以使用'.scrollTo(position)'函数 – hrskrs

回答

0

首套一切滚动型 内,现在把现在按一下按钮动作集这样

<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> 

一个的LinearLayout里面的按钮和一个TextView你的文本到textView,不要忘记将其的可见性更改为可见。最后滚动到与scrollView.fullScroll(View.FOCUS_DOWN)TextView的

+0

谢谢!我会在下午晚些时候尝试一下:) –

0

-----对于自动滚动网页视图点击切换按钮----

public class MainActivity extends AppCompatActivity { 
    TimerTask mTimerTask; 
    final Handler handler = new Handler(); 
    Timer t = new Timer(); 
    SharedPreferences pref; 
    SharedPreferences.Editor editor; 
    String key; 
    int getValue; 
    @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{ 
        //store value in sharedpreference 
        pref= getApplicationContext().getSharedPreferences("mypref", 0); 
        int value = webView.getScrollY() ; 
        editor= pref.edit(); 
        editor.putInt("key",value); 
        editor.commit(); 
        //get value in shared preference 
        getValue = pref.getInt("key", 0); 
        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); 
      } 

     }); 
    } 
}