gpt4 book ai didi

java - 如何将 ScrollView 添加到我的 xml 中?

转载 作者:行者123 更新时间:2023-12-01 17:59:55 25 4
gpt4 key购买 nike

你好,我做了一个 ListView ,在 itemclick 上它让我进入另一个 Activity 来阅读一堆文本,但我有一个问题。如果我在 appdata.java 中添加长文本,我将无法看到所有内容,因为我无法向下滚动,基本上我想要的是创建一个 ScrollView ,以便我能够读取所有文本。

detail_activity

package com.example.test.testing.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.test.testing.R;

public class DetailActivity extends AppCompatActivity {
private static final String BUNDLE_EXTRAS = "BUNDLE_EXTRAS";
private static final String EXTRA_QUOTE = "EXTRA_QUOTE";
private static final String EXTRA_ATTR = "EXTRA_ATTR";


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

Bundle extras = getIntent().getBundleExtra(BUNDLE_EXTRAS);

((TextView)findViewById(R.id.lbl_quote_text)).setText(extras.getString(EXTRA_QUOTE));
((TextView)findViewById(R.id.lbl_quote_attribution)).setText(extras.getString(EXTRA_ATTR));

}
}

activity_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_detail"
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.test.testing.ui.DetailActivity">

<TextView
android:id="@+id/lbl_quote_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium" />

<TextView
android:id="@+id/lbl_quote_attribution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lbl_quote_text"
android:fontFamily="sans-serif-light"
android:textStyle="italic" />

</RelativeLayout>

appdata.java

package com.example.test.testing.model;

import com.example.test.testing.R;

import java.util.ArrayList;
import java.util.List;


public class appData {
private static final String[] titles = {"Nothingness cannot be defined",
"Time is like a river made up of the events which happen, and a violent stream; " +
"for as soon as a thing has been seen, it is carried away, and another comes" +
" in its place, and this will be carried away too,",
"But when I know that the glass is already broken, every minute with it is precious.",
"For me, it is far better to grasp the Universe as it really is than to persist in" +
" delusion, however satisfying and reassuring.",
"The seeker after the truth is not one who studies the writings of the ancients and," +
" following his natural disposition, puts his trust in them, but rather the" +
" one who suspects his faith in them and questions what he gathers from them," +
" the one who submits to argument and demonstration, and not to the " +
"sayings of a human being whose nature is fraught with all kinds " +
"of imperfection and deficiency.",
"You must take personal responsibility. You cannot change the circumstances, the" +
" seasons, or the wind, but you can change yourself. That is something you" +
" have charge of."
};
private static final String[] subTitles = {"Bruce Lee",
"Marcus Aurelius",
"Meng Tzu",
"Ajahn Chah",
"Carl Sagan",
"Alhazen",
"Jim Rohn"

};
private static final int icon = R.drawable.ic_tonality_black_36dp;

public static List<ListItem> getListData() {
List<ListItem> data = new ArrayList<>();

//Repeat process 4 times, so that we have enough data to demonstrate a scrollable
//RecyclerView
for (int x = 0; x < 4; x++) {
//create ListItem with dummy data, then add them to our List
for (int i = 0; i < titles.length; i++) {
ListItem item = new ListItem();
item.setTitle(titles[i]);
item.setSubTitle(subTitles[i]);
data.add(item);
}
}
return data;
}
}

最佳答案

这可能对您有帮助:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_detail"
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.test.testing.ui.DetailActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/lbl_quote_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium" />

<TextView
android:id="@+id/lbl_quote_attribution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lbl_quote_text"
android:fontFamily="sans-serif-light"
android:textStyle="italic" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

最好在ScrollView中使用线性布局来设置正确的方向

关于java - 如何将 ScrollView 添加到我的 xml 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41833390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com