繁体   English   中英

从Android中的XML布局获取视图不起作用

[英]Get View From XML Layout in Android Doesn't work

我对如何从Android的XML布局中获取视图有疑问? 我正在使用微调器,它现在可以保存选定的微调器值。 我想从content_doa.xml布局中获取ID为titleTextView的TextView的视图。 我尝试使用LayoutInflater,但是它不起作用,还没有任何效果。 你能解决我的问题吗?

干得好:

FontSettings.java

public class FontSettings extends AppCompatActivity {

private Spinner spinner1, spinnerLatin;
private SharedPreferences mMyPrefs;
private SharedPreferences.Editor mMyEdit;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_font);

    // toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //this line shows back button

    //Display data size teks arab in dropdown list spinner
    final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);
    final LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final SharedPreferences sharedPref = getSharedPreferences("My_Prefs", 6);
    ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);
    spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);
    spinnerBackgroundChange.setSelection(sharedPref.getInt("VALUE", 6));

    //Save spinner value
    spinnerBackgroundChange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            ((TextView) view).setTextColor(Color.parseColor("#226169"));

            switch(position) {
                case 0:
                    SharedPreferences.Editor editor0 = sharedPref.edit();
                    editor0.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor0.commit();

                    Activity activity = (Activity) parent.getContext();
                    TextView dgs = (TextView) activity.findViewById(R.id.tekzArab);
                    dgs.setTextSize(12);
                    break;

                case 1:
                    SharedPreferences.Editor editor1 = sharedPref.edit();
                    editor1.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor1.commit();

                    Activity activitys = (Activity) parent.getContext();
                    TextView dgf = (TextView) activitys.findViewById(R.id.tekzArab);
                    dgf.setTextSize(14);

                    View contentView = inflater.inflate(R.layout.content_doa, null,false);
                    TextView titleMessage = (TextView) contentView.findViewById(R.id.titleTextView);
                    titleMessage.setTextSize(14);
                    break;

                case 2:
                    SharedPreferences.Editor editor2 = sharedPref.edit();
                    editor2.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor2.commit();

                    Activity activityz = (Activity) parent.getContext();
                    TextView dgy = (TextView) activityz.findViewById(R.id.tekzArab);
                    dgy.setTextSize(16);
                    break;

                case 3:
                    SharedPreferences.Editor editor3 = sharedPref.edit();
                    editor3.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor3.commit();

                    Activity activitye = (Activity) parent.getContext();
                    TextView dgq = (TextView) activitye.findViewById(R.id.tekzArab);
                    dgq.setTextSize(18);
                    break;

                case 4:
                    SharedPreferences.Editor editor4 = sharedPref.edit();
                    editor4.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor4.commit();

                    Activity activityq = (Activity) parent.getContext();
                    TextView dgp = (TextView) activityq.findViewById(R.id.tekzArab);
                    dgp.setTextSize(20);
                    break;

                case 5:
                    SharedPreferences.Editor editor5 = sharedPref.edit();
                    editor5.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor5.commit();

                    Activity activityc = (Activity) parent.getContext();
                    TextView dgn = (TextView) activityc.findViewById(R.id.tekzArab);
                    dgn.setTextSize(22);
                    break;

                case 6:
                    SharedPreferences.Editor editor6 = sharedPref.edit();
                    editor6.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor6.commit();

                    Activity activitym = (Activity) parent.getContext();
                    TextView dgb = (TextView) activitym.findViewById(R.id.tekzArab);
                    dgb.setTextSize(24);
                    break;

                case 7:
                    SharedPreferences.Editor editor7 = sharedPref.edit();
                    editor7.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor7.commit();

                    Activity activityo = (Activity) parent.getContext();
                    TextView dgl = (TextView) activityo.findViewById(R.id.tekzArab);
                    dgl.setTextSize(26);
                    break;

                default:
                    Activity activityu = (Activity) parent.getContext();
                    TextView dgx = (TextView) activityu.findViewById(R.id.tekzArab);
                    dgx.setTextSize(24);
                    break;

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


    //Display data size teks latin in dropdown list spinner
    Spinner spinnerLatin = (Spinner)findViewById(R.id.spinnerLatin);
    ArrayAdapter<CharSequence> spinnerArrayLatin = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    spinnerArrayLatin.setDropDownViewResource(R.layout.textview_with_background);
    spinnerLatin.setAdapter(spinnerArrayLatin);
    // spinnerLatin default value
    spinnerLatin.setSelection(1);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
} }

settings_font.xml

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#226169"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_collapseMode="pin"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- Font Arab -->
        <!-- Judul -->
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reldoa"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/fontArab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="15dp"
                android:paddingLeft="15dp"
                android:paddingTop="15dp"
                android:text="Font Arab"
                android:textColor="#226169"
                android:textSize="20sp" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reldoa"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tekzArab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:paddingBottom="20dp"
                android:paddingRight="10dp"
                android:paddingTop="20dp"
                android:text="أسْتَغْفِرُ اللهَ العَظِيمَ"
                android:textColor="#222222" />

        </RelativeLayout>


        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reldoa"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="3dp"
    android:background="@android:color/white"
    android:orientation="vertical">

            <TextView
                android:id="@+id/sizedoa"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:paddingTop="5dp"
                android:text="Ukuran Font"
                android:textColor="#222222"
                android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reldoa"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="3dp"
    android:background="@android:color/white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/sizelatin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingTop="5dp"
        android:text="Jenis Font"
        android:textColor="#222222"
        android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:entries="@array/type_arrays"
        android:prompt="@string/type_font"/>

</RelativeLayout>

        <!-- Font latin -->

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/relLatin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="45dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/fontLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="15dp"
                android:paddingLeft="15dp"
                android:paddingTop="15dp"
                android:text="Font Latin"
                android:textColor="#226169"
                android:textSize="20sp" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reldoa"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tekzLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="20dp"
                android:paddingRight="10dp"
                android:paddingLeft="15dp"
                android:paddingTop="20dp"
                android:text="Aku meminta ampunan kepada Allahَ"
                android:textColor="#222222" />

        </RelativeLayout>


        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/relLatin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/sizeLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:paddingTop="5dp"
                android:text="Ukuran Font"
                android:textColor="#222222"
                android:textSize="18sp" />

            <Spinner
                android:id="@+id/spinnerLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:entries="@array/country_arrays"
                android:prompt="@string/country_prompt" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reldoa"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tipeLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:paddingTop="5dp"
                android:text="Jenis Font"
                android:textColor="#222222"
                android:textSize="18sp" />

            <Spinner
                android:id="@+id/spinnerTipe"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:entries="@array/type_arrays"
                android:prompt="@string/type_font"/>

        </RelativeLayout>

    </LinearLayout>
</ScrollView>

content_doa.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/coordinatorLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/colorButtonNormal">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical"
    android:padding="5dp"
    android:background="?attr/colorButtonNormal">

    <!-- TextView Nama Mahasiwa -->
    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="15sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:textColor="#222222"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/readmore"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="15sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:text="Baca selengkapnya"
        android:textColor="#000ecc"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/latinDoa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="8sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:textColor="@android:color/holo_green_dark"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/artiDoa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="8sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:textColor="#222222" />

    <TextView
        android:id="@+id/sumberDoa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="8sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:textColor="#222222" />

    <TextView
        android:id="@+id/readless"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="15sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:text="Baca lebih sedikit"
        android:textColor="#000ecc"
        android:textSize="14sp" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relmasjid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <Button
            android:id="@+id/buttonbaca"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />

        <Button
            android:id="@+id/buttoncopy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Salin" />

        <Button
            android:id="@+id/buttonshare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:actionProviderClass="android.widget.ShareActionProvider"
            android:text="Bagikan" />

    </RelativeLayout>

</LinearLayout>

已更新,带有指向已解决问题的链接抱歉,没有代码。 到家时,我可以添加几行,但最好的选择是3号,这是您需要研究和使用的。

您不能直接与其他活动的视图进行交互。 该活动布局不会膨胀,因此您只会遇到空指针异常。 您必须间接执行此操作。 有3种方法可以立即实现。

  1. SharedPreferences像现在一样在微调器中设置共享首选项中的值。 但是在activitycontent.java中,您可以获取共享的首选项,并查看值是否位于某个键值对中。 如果是这样,则在activitycontent.java中将其文本大小设置为该值。 如果它为null或为空,那么它将是您已将contentdoa.xml值设置为它的值。 这将是最简单的,因为您无需为数据库编写顶级代码,也不必完全重新编写整个应用程序

Activitycontent.java

将此添加到onCreate

SharedPreferences sp = getSharedPreferences("My_Prefs", Activity.MODE_PRIVATE); 
 int titleTextSize = sp.getInt("VALUE", 24);
TextView titleTV = (TextView) findViewById(R.id.titleTextView);
titleTV.setTextSize(titleTextSize);

以编程方式设置TextViewSize

查看该帖子,并仔细阅读答案和评论。 您还可以尝试的另一件事是在线性布局中添加I,然后先引用它,然后再引用类似的东西。

linearLayout.findViewById..... you know how the rest goes
  1. 创建一个SQLite数据库并在其中存储您的值。 对于您所有的修改以及在每个活动中可以更改的任何内容上,您都拥有视图活动,请检查是否设置了值(如果已设置),则将视图textSize设置为该值。 由于您有很多可以更改的地方,因此我建议您更多。
  2. 我什至建议您进一步研究并将其用于您的应用设置。 应用程序设置的默认SharedPrefences

好的,我现在知道了。 您的资源中有一个数组列表,用于填充微调框。 单击该项目后,代码将获取该项目在数组列表中的位置,并将其设置为共享首选项,然后在ActivityContent.java中进行访问。

在ActivityContent.java中,您从共享首选项中获得的价值是该项目在arraylist中的位置。

首先,为了帮助您学习,我将解释您可以如何做。 接下来,我将解释您打算做什么。

第一

您在arraylist中具有item的位置,因此可以在ActivityContent.java类中执行此操作。

ActivityContent.java

String[] stringArray = getResources().getStringArray(R.array.country_arrays)
SharedPreferences sp = getSharedPreferences("My_Prefs", Activity.MODE_PRIVATE); 
int sizeItemSelected= sp.getInt("VALUE", 24);

//if array contains strings if not just make this variable an Int
String sizeToSetTextViewSizeTo = stringArray[sizeItemSelected];
int tvSize = Integer.parseInt(sizeToSetTextViewSizeTo);


TextView titleTV = (TextView) findViewById(R.id.titleTextView);
titleTV.setTextSize(tvSize);

第二

您只需传递选定的项(您要在activitycontent中将textview设置为的大小)来代替传递位置。

我认为您打算将代码放在首位

case 1:
SharedPreferences.Editor editor1 = sharedPref.edit();
editor1.putInt("VALUE", spinnerBackgroundChange.getSelectedItem());                   
editor1.commit();
break;

代替这个

case 1:
SharedPreferences.Editor editor1 = sharedPref.edit();
editor1.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());                   
editor1.commit();
break;

唯一的事情是我从来没有见过这样的案例:它本身

OnItemSelectedListender()

如你所愿。 但是我不明白为什么这会影响代码的“第一”或“第二”选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM