簡體   English   中英

ScrollView無法正常工作或在Fragment中顯示欄

[英]ScrollView not working or showing bar in Fragment

我有一個自定義片段,其中包含ScrollViewTextView ,這是我一生都無法滾動的……這應該是一個簡單的控制台,可以向上滾動到以前的文本和其他垃圾,但始終在屏幕上顯示新文本。底部帶有滾動鎖定選項(尚未實現)。 在啟動時, TextView為空,但是有一個方法可以添加並最終從主Activity中填充TextView 除滾動外,所有其他功能均有效。 下面是代碼片段。 我從研究中嘗試了無數的鏈接,甚至結合其他鏈接的嘗試,也嘗試了幾乎所有遇到的內容。 依然沒有。 總而言之,我只想在垂直滾動的底部添加文本。 那么關於正確實現ScrollView任何建議? 感謝所有的幫助,非常感謝。

另外,我不確定它是否添加了iv嘗試的所有鏈接,但均無法正常工作... iv保存所有選項卡只是為了防止有人找到我無法找到的鏈接。

CustomFragment

public class CustomFragment extends Fragment {
    ScrollView scrollView;
    TextView textView;

    public void appendText(CharSequence text) {
        textView.append(text);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        textView = new TextView(getActivity());
        RelativeLayout.LayoutParams viewLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(viewLayoutParams);
        textView.setVerticalScrollBarEnabled(true);

        scrollView = new ScrollView(getActivity());
        RelativeLayout.LayoutParams scrollLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
        scrollView.setLayoutParams(scrollLayoutParams);
        scrollView.setFillViewport(true);
        scrollView.setScrollContainer(false);
        scrollView.addView(textView);

        return scrollView;
    }
}

主布局

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

從活動方法加載片段

FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(id, fragment);
fragmentTransaction.commit();

我建議您嘗試使用XML編寫布局,並在運行時對它們進行膨脹,例如99%的Android應用程序(包括Google的應用程序)。

了解布局工作方式后,您可以掌握以編程方式創建布局的知識。

調試(僅查看/更改)XML比不知道它做什么(或正在做什么)的編程代碼容易。 在對XML進行存根處理之后,您可以決定是否值得(大多數情況下不值得)移植到代碼中。

我花了三分鍾

<?xml version="1.0" encoding="utf-8"?>
<merge 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:parentTag="FrameLayout">

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

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." />
 </ScrollView>
</merge>

更新

您實際需要綁定 TextView才能引用它。 我想在某個時候您會增加布局 因此,您的XML應該具有android:id=“@+id/your_textview_id這樣您就可以執行TextView tv = (TextView) view.findViewById(R.id.your_textview_id);

因此,現在您有了引用( tv ),並且可以執行tv.setText(String) (或追加或任何您想調用的東西)。

我相信您需要退后一步,並直接從Google閱讀有關Android基礎知識的文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM