我已经通过将文本加载到片段中来加载ViewPager中的文本。在片段内部,有一个WebView。这是用View setFragmentView编写的。
我需要动态更改字体的大小。
所以我添加了一个搜索栏,并且在触摸搜索栏时,尺寸应该改变(现在)。
但我当前加载的页面不会改变其字体大小。
ViewPager中的下一页已更新。
他们是否可以重新加载整个片段。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeFont("font/Raleway-Light.ttf");
rlFontSettings = findViewById(R.id.rl_font_settings);
rlFontSettings.setVisibility(View.GONE);
sbVolumeController = findViewById(R.id.sb_volume_controller);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
pxScreenWidth = getResources().getDisplayMetrics().widthPixels;
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setOffscreenPageLimit(0);
mViewPager.setAdapter(mSectionsPagerAdapter);
sbVolumeController.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d("year", "onStartTrackingTouch: inside the seek 1 ");
changedVolumeValue = 50;
webSettings.setDefaultFontSize(changedVolumeValue);
mViewPager.setCurrentItem(currentPage);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
if (getIntent() != null && getIntent().getExtras() != null) {
String filePath = getIntent().getExtras().getString("filePath");
isPickedWebView = getIntent().getExtras().getBoolean("isWebView");
try {
reader = new Reader();
//Setting optionals once per file is enough.
reader.setMaxContentPerSection(1250);
reader.setCssStatus(isPickedWebView ? CssStatus.INCLUDE : CssStatus.OMIT);
reader.setIsIncludingTextContent(true);
reader.setIsOmittingTitleTag(true);
// This method must be called before readSection.
reader.setFullContent(filePath);
// int lastSavedPage = reader.setFullContentWithProgress(filePath);
if (reader.isSavedProgressFound()) {
int lastSavedPage = reader.loadProgress();
mViewPager.setCurrentItem(lastSavedPage);
}
} catch (ReadingException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Override
public View onFragmentReady(int position) {
BookSection bookSection = null;
try {
bookSection = reader.readSection(position);
} catch (ReadingException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
} catch (OutOfPagesException e) {
e.printStackTrace();
this.pageCount = e.getPageCount();
if (isSkippedToPage) {
Toast.makeText(MainActivity.this, "Max page number is: " + this.pageCount, Toast.LENGTH_LONG).show();
}
mSectionsPagerAdapter.notifyDataSetChanged();
}
isSkippedToPage = false;
if (bookSection != null) {
return setFragmentView(isPickedWebView, bookSection.getSectionContent(), "text/html", "UTF-8"); // reader.isContentStyled
}
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.action_search);
fontSettings = menu.findItem(R.id.action_font);
searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
fontSettings.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
rlFontSettings.setVisibility(View.VISIBLE);
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (query != null && !query.equals("")) {
if (TextUtils.isDigitsOnly(query)) {
loseFocusOnSearchView();
int skippingPage = Integer.valueOf(query);
if (skippingPage >= 0) {
isSkippedToPage = true;
mViewPager.setCurrentItem(skippingPage);
} else {
Toast.makeText(MainActivity.this, "Page number can't be less than 0", Toast.LENGTH_LONG).show();
}
} else {
loseFocusOnSearchView();
Toast.makeText(MainActivity.this, "Only numbers are allowed", Toast.LENGTH_LONG).show();
}
return true;
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
@Override
public void onBackPressed() {
if (!searchView.isIconified()) {
loseFocusOnSearchView();
} else {
super.onBackPressed();
}
}
@Override
protected void onStop() {
super.onStop();
try {
reader.saveProgress(mViewPager.getCurrentItem());
Toast.makeText(MainActivity.this, "Saved page: " + mViewPager.getCurrentItem() + "...", Toast.LENGTH_LONG).show();
} catch (ReadingException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Progress is not saved: " + e.getMessage(), Toast.LENGTH_LONG).show();
} catch (OutOfPagesException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Progress is not saved. Out of Bounds. Page Count: " + e.getPageCount(), Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
private View setFragmentView(boolean isContentStyled, String data, String mimeType, String encoding) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
isReContentStyled = isContentStyled;
reData = data;
reMimeType =mimeType;
reEncoding = encoding;
if (isContentStyled) {
webView = new WebView(MainActivity.this);
webSettings = webView.getSettings();
Log.d("year", "setFragmentView: mytest 1 "+changedVolumeValue);
webSettings.setDefaultFontSize(changedVolumeValue);
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
Log.d("year", "setFragmentView: mytest 2 "+changedVolumeValue);
webView.loadDataWithBaseURL(null, data, mimeType, encoding, null);
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
// }
webView.setLayoutParams(layoutParams);
return webView;
} else {
ScrollView scrollView = new ScrollView(MainActivity.this);
scrollView.setLayoutParams(layoutParams);
final TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(layoutParams);
textView.setTextSize(changedVolumeValue);
textView.setText(Html.fromHtml(data, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
String imageAsStr = source.substring(source.indexOf(";base64,") + 8);
byte[] imageAsBytes = Base64.decode(imageAsStr, Base64.DEFAULT);
Bitmap imageAsBitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
int imageWidthStartPx = (pxScreenWidth - imageAsBitmap.getWidth()) / 2;
int imageWidthEndPx = pxScreenWidth - imageWidthStartPx;
Drawable imageAsDrawable = new BitmapDrawable(getResources(), imageAsBitmap);
imageAsDrawable.setBounds(imageWidthStartPx, 0, imageWidthEndPx, imageAsBitmap.getHeight());
return imageAsDrawable;
}
}, null));
int pxPadding = dpToPx(12);
textView.setPadding(pxPadding, pxPadding, pxPadding, pxPadding);
scrollView.addView(textView);
return scrollView;
}
}
private void loseFocusOnSearchView() {
searchView.setQuery("", false);
searchView.clearFocus();
searchView.setIconified(true);
MenuItemCompat.collapseActionView(searchMenuItem);
}
private int dpToPx(int dp) {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return pageCount;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
currentPage = position;
return PageFragment.newInstance(position);
}
}
答案 0 :(得分:0)
您可以使用xml属性:
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform" />
这是在Android 8中添加的,但它包含在支持库中。在此处查找更多信息:https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html