gpt4 book ai didi

android - 如何使用首选项 Activity 访问布局中设置的小部件

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:43 24 4
gpt4 key购买 nike

我正在创建首选项 Activity ,因为我正在使用布局添加自定义首选项屏幕并且我想动态更新值。

代码如下:

 <PreferenceCategory
android:key="more_category"
android:title="More Wallpapers " >
<Preference
android:key="more_apps"
android:layout="@layout/more_apps" />

这是我的布局 more_apps.xml

   <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_height="match_parent"
>

<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/image_one"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/description_text"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_below="@+id/title_text"
android:layout_toRightOf="@+id/image_one"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
android:id="@+id/image_one"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/launcher" />

我想在运行时更新我的​​ TextView 和图像,如何在首选项 Activity 中访问?

请帮忙

最佳答案

您需要像下面的代码一样在 xml 布局中使用您提到的偏好类

<PreferenceCategory
android:key="more_category"
android:title="More Wallpapers " >

<com.myapp.myclass android:key="more_apps"/>

</PreferenceCategory>

com.myapp 是包名,myclass 是所有代码所在的 java 文件。

这是 java 代码和膨胀的 xml more_apps.xml

  public class myclass extends Preference {

public myclass(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
protected View onCreateView(ViewGroup parent) {
// TODO Auto-generated method stub

LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = inflater.inflate(R.layout.more_apps, null);

return view;
}

}

这样您就可以创建自定义偏好 Activity 并执行任何操作..

关于android - 如何使用首选项 Activity 访问布局中设置的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10997007/

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