【问题标题】:setTextSize method doesn't work by using LayoutInflater :使用 LayoutInflater 时 setTextSize 方法不起作用:
【发布时间】:2019-02-27 14:39:29
【问题描述】:

当我单击 textView(不包含在以下代码中)时,我想实现 changeTextSize1 方法(在 SettingActivity.java 中)。 changeTextSize1是改变finalT textView的textsize的方法。 但它不起作用。

SettingActivity.java

 public void changeTextSize1(View v) {

    TextView ts1 = (TextView) findViewById(R.id.textsize1);
    TextView ts2 = (TextView) findViewById(R.id.textsize2);
    TextView ts3 = (TextView) findViewById(R.id.textsize3);
    ts1.setTypeface(ts1.getTypeface(), Typeface.BOLD);
    ts2.setTypeface(ts2.getTypeface(), Typeface.NORMAL);
    ts3.setTypeface(ts3.getTypeface(), Typeface.NORMAL);

    View view = (View) getLayoutInflater().inflate(R.layout.recycler_todo, null);

    TextView finalT = (TextView) view.findViewById(R.id.finalText);
    finalT.setTextSize(22);
}

recycler_todo.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#56A2B7"
android:orientation="vertical">

<TextView
    android:id="@+id/finalText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:layout_marginBottom="25dp"
    android:gravity="center"
    android:text="text"
    android:textSize="16sp"
    android:textColor="#ffffff"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:background="#ffffff"
    android:orientation="vertical">

</LinearLayout>

下面是更多代码。

MyAdapter.java

public MyAdapter(ArrayList<TodoList> myDataset) {
    mDataset = myDataset;
}

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    LinearLayout v = (LinearLayout) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.recycler_todo, parent, false);
    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

activity_main.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="672dp"
    android:orientation="vertical"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#56A2B7">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

enter image description here

enter image description here

第一张图片:如果我点击那个“1”TextView... → 第二张图片:那句话需要更大...

需要更多代码吗?

【问题讨论】:

  • 你到底在哪里使用recycler_todo.xml
  • 这是我项目的主要recyclerview。与 MainActivity 链接。
  • 如果 recycler_todo 是您的 RecyclerView 的一个项目,那么您不应该以这种方式访问​​其内部视图。
  • 我是Android Studio的初学者。所以,我不确定 xml 文档是我的 RecyclerView 的一个项目。 (但我在 MainActivity 中使用 ArrayList 放置项目)感谢您的建议。
  • 如果我了解您所面临的确切情况,我愿意提供帮助。发布更多代码,以便我可以得到你想要做的事情并找到罪魁祸首。

标签: android android-layout textview


【解决方案1】:

哦,首先,您要从“设置”屏幕更改项目的文本大小,以反映在适配器中。

然后,您只需将该文本大小保存到 SharedPreferences,然后在 MyAdapter onBindViewHolder 方法中读取设置中的文本大小,并设置为您的文本视图,就这么简单。

这是一个小问题,

public MyAdapter(ArrayList<TodoList> myDataset) {
    mDataset = myDataset;
}

@Override
public MyAdapter.MyViewHolder onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {

    // HERE YOUR CODE GOES TO SET TEXT SIZE FROM SETTING
    holder.textview.setText(readSizeFromSharedPreference())
}

【讨论】:

  • 它不起作用..我认为java源代码不是主要问题。我找不到的另一个原因肯定是存在的。谢谢你的建议。
  • 哦,我的朋友,您只是膨胀了一个布局,并更改了它的文本大小,您没有显示任何用于在任何容器中注入该视图的代码。显然,它还没有添加到 UI 中。
  • 哦..我明白了...小...嗯...你的意思是..我需要制作可以查看我使用充气机制作的容器?你能给我一些示例代码吗?太难了……
猜你喜欢
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
相关资源
最近更新 更多