【发布时间】:2019-04-11 05:42:51
【问题描述】:
我使用设置活动让用户从我的 Android 应用程序中的不同资源中进行选择。所有首选项都是复选框。我为每个首选项定义了一个自定义布局并将其连接到复选框。当我单击首选项时,它工作正常,但我不明白首选项在哪个位置。所以我想在单击首选项时以编程方式更改自定义布局的图像视图。有没有办法做到这一点?
下面,第二项是默认文本,我想把布局改成第一项。一切正常,但是当我单击此项目时,我想以编程方式将加号图像更改为另一个图像(以了解它已被选中)。由于这是设置活动,我找不到方法(没有 findviewbyid 等。)Android 将整个自定义布局视为一个复选框(您可以单击该行的任意位置)
设置.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:layout="@layout/source_item"
android:key="gundem_haberturk"
android:title="@string/haberturk"
android:defaultValue="false"/>
</PreferenceScreen>
自定义布局.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_source_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_source_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="100dp"
android:maxHeight="40dp"
android:src="@drawable/logo_haberturk"/>
<TextView
android:id="@+id/tv_source_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Habertürk"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/iv_source_selector"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/checkbox_unchecked"/>
</RelativeLayout>
设置片段:
public class GundemSettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String rootKey) {
addPreferencesFromResource(R.xml.gundem_settings);
}
}
设置活动:
public class GundemSettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gundem_settings);
Toolbar toolbar = findViewById(R.id.settings_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}
设置活动 xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hhs.haberler.Settings.GundemSettingsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/settings_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<fragment
android:layout_marginTop="?attr/actionBarSize"
android:id="@+id/settings_fragment"
android:name="com.example.haberler.Settings.GundemSettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
【问题讨论】:
标签: android android-layout settings android-preferences