มีวิธีใดในการเพิ่มปุ่มที่ด้านล่างของหน้าจอการตั้งค่าและทำให้มันทำงานได้อย่างถูกต้องเมื่อเลื่อน
มีวิธีใดในการเพิ่มปุ่มที่ด้านล่างของหน้าจอการตั้งค่าและทำให้มันทำงานได้อย่างถูกต้องเมื่อเลื่อน
คำตอบ:
มีอีกวิธีหนึ่งสำหรับการปรับแต่งรูปลักษณ์ของค่ากำหนด
ออกแบบเค้าโครง XML ปกติด้วยปุ่มหรืออะไรก็ได้ที่คุณต้องการเพิ่มลงในค่ากำหนดมาตรฐาน รวมถึงในรูปแบบของคุณและให้ประชาชนListView@android:id/list
สมมติว่าเราเรียกไฟล์เลย์เอาres/layout/main.xmlต์ อาจมีลักษณะดังนี้:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <Button android:text="This is a button on top of all preferences."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <ListView android:id="@android:id/list"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
</LinearLayout>
ในของคุณPreferenceActivityให้เพิ่มสองบรรทัดนี้ในonCreate:
addPreferencesFromResource(R.xml.preferences);
setContentView(R.layout.main);
ในรูปแบบของคุณแล้วจะถูกแทนที่ด้วยการตั้งค่าที่กำหนดไว้ทางปกติในListViewres/xml/preferences.xml
ฉันรู้ว่ามันช้าไปหน่อย แต่ฉันเพิ่งพบวิธีแก้ปัญหาที่ฉันชอบดีกว่าโซลูชันที่ได้รับการยกย่องของ Max
คุณสามารถเพิ่มส่วนท้าย (หรือถ้าคุณต้องการให้ปุ่มอยู่ด้านบนส่วนหัว) ใน ListView ของ PreferenceActivity ดังนี้:
public class MyActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        ListView v = getListView();
        v.addFooterView(new Button(this));
    }
}
ฉันหวังว่านี่จะช่วยใครบางคนได้
ListViewด้วย PreferenceFragment :(
                    PreferenceFragmentมีรายชื่อของตัวเอง) แต่ฉันมันแก้ไขได้โดยเพียงแค่การสร้างPreferenceและการตั้งค่าIntentเกี่ยวกับมัน ไม่จำเป็นต้องสร้างปุ่ม (อย่างน้อยก็ในกรณีของฉัน)
                    ตัวอย่างด้านล่างนี้จะแสดงปุ่มที่ด้านล่างของหน้า (ในกรณีที่ยังมีใครสนใจ)
ในกรณีของ LinearLayout คุณสามารถใช้น้ำหนักได้ด้วย สิ่งนี้จำเป็นเนื่องจาก Listview ถูกตั้งค่าเป็น * fill_parent * ฉันมักจะทำโดยเพิ่ม * android: layout_weight *:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">
    <ListView android:id="@android:id/list"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" android:layout_weight="10"/>
    <Button android:text="This is a button on top of all preferences."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_weight="1"/>
</LinearLayout>
คำอธิบายด้านล่างอาจไม่ได้ 100% แต่จะช่วยให้คุณเข้าใจ ...
+-- View Port (linear layout)
| +-- List View (this is where the preferences will go)
| |
| |
| +--
+--
  +--
  | Button (which was pushed out of view by the fillparent of ListView
  +--
คุณยังสามารถพูดได้ว่าเพราะปุ่มไม่มีน้ำหนัก ปุ่มนี้แสดงผลที่ความสูง 0dp
ตอนนี้มีการเพิ่ม layout_weigths มันจะปล่อยให้ปุ่มแสดงผลในมุมมอง
+-- View Port (linear layout)
| +-- List View (this is where the preferences will go)
| |
| |
| +--
| +--
| | Button (which was pushed out of view by the fillparent of ListView
| +--
+--
              จริงๆแล้วมีทางแก้ นี่คือรหัสฉันหวังว่านี่จะเป็นประโยชน์สำหรับทุกคน ดูเหมือน 3 ตัวเลือกและ 2 ปุ่มที่ด้านล่างของหน้าจอโดยไม่ขึ้นกับความละเอียดหน้าจอ (กำหนดเป้าหมายเป็น 240 เป็นต่ำสุด)
package com.myapplication.gui;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.view.Display;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import com.myproject.general.HeightListView;
import com.myapplication.R;
public class FilterActivity extends PreferenceActivity {
    private LinearLayout rootView; 
    private LinearLayout buttonView; 
    private Button buttonDone;
    private Button buttonRevert;
    private ListView preferenceView; 
    private LinearLayout gradientView;
    private ScrollView scrollRoot;
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
        int height = display.getHeight();
        int width = height > 240 ? display.getWidth() : display.getWidth() - 4;
        scrollRoot = new ScrollView(this);
        scrollRoot.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        rootView = new LinearLayout(this); 
        rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        rootView.setOrientation(LinearLayout.VERTICAL);
        buttonView = new LinearLayout(this); 
        buttonView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        buttonView.setOrientation(LinearLayout.HORIZONTAL);
        buttonView.setGravity(Gravity.BOTTOM);
        gradientView = new LinearLayout(this);
        gradientView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        gradientView.setOrientation(LinearLayout.HORIZONTAL);
        gradientView.setBackgroundResource(R.drawable.gradient);
        gradientView.setPadding(0, 5, 0, 0);
        gradientView.setBackgroundResource(R.drawable.gradient);
        buttonDone = new Button(this); 
        buttonDone.setText(R.string.filterButton_Done); 
        buttonDone.setLayoutParams(new LayoutParams(width/2, LayoutParams.WRAP_CONTENT));
        gradientView.addView(buttonDone);
        buttonRevert = new Button(this); 
        buttonRevert.setText(R.string.filterButton_Revert);
        buttonRevert.setLayoutParams(new LayoutParams(width/2, LayoutParams.WRAP_CONTENT));
        gradientView.addView(buttonRevert);
        buttonView.addView(gradientView);
        preferenceView = new HeightListView(this); 
        preferenceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
        preferenceView.setId(android.R.id.list); 
        PreferenceScreen screen = createPreferenceHierarchy(); 
        screen.bind(preferenceView); 
        preferenceView.setAdapter(screen.getRootAdapter()); 
        rootView.addView(preferenceView);
        rootView.addView(buttonView);
        if (height > 240) {
            this.setContentView(rootView);
        }
        else {
            scrollRoot.addView(rootView);
            this.setContentView(scrollRoot);
        }
        setPreferenceScreen(screen); 
    } 
    private PreferenceScreen createPreferenceHierarchy() {        
        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
        PreferenceScreen pref1 = getPreferenceManager().createPreferenceScreen(this);
        pref1.setKey("pref1");
        pref1.setTitle("Title");
        pref1.setSummary("Summary");
        root.addPreference(pref1); 
        PreferenceScreen pref2 = getPreferenceManager().createPreferenceScreen(this);
        pref2.setKey("pref2");
        pref2.setTitle("Title");
        pref2.setSummary("Summary");
        root.addPreference(pref2); 
        PreferenceScreen pref3 = getPreferenceManager().createPreferenceScreen(this);
        pref3.setKey("pref3");
        pref3.setTitle("Title");
        pref3.setSummary("Summary");
        root.addPreference(pref3); 
        return root; 
    } 
}
              คุณต้องใช้ PreferenceFragment ภายในกิจกรรมทั่วไปและเพิ่มปุ่มลงในเค้าโครงกิจกรรม
public class SettingActivity extends Activity {
    UserProfileViewModel userProfileViewModel = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        getFragmentManager().beginTransaction()
                .replace(R.id.content, new SettingsFragment())
                .commit();
    }
    private class SettingsFragment extends PreferenceFragment {
        public SettingsFragment() {
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.pref_main);
        }
    }
}
SettingActivity.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/buttonSave"/>
    <Button
        android:id="@+id/buttonSave"
        android:text="Save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
activity_setting
buttonSave:)
                    นี่จะเป็นลักษณะของโค้ดในกิจกรรมที่ตัวอย่างของ ronny ความตั้งใจของฉันคือการวางเมนูไว้ที่ด้านล่างของหน้าจอ
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.prefs);
    addPreferencesFromResource(R.xml.prefs);
   /* LayoutInflater CX = getLayoutInflater();
    CX.inflate(R.layout.main,null);*/
    // TODO Auto-generated method stub
}
               <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="@dimens/listview_height" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="This is a button on top of all preferences." />
</RelativeLayout>
ฉันอ้างอิง @Ronnie ใช้ RelativeLayout และตั้งค่าความสูงสำหรับ layout_height ของ listview จากนั้นตั้งค่า layout_alignParentBottom = "true" ของปุ่มมันสามารถแสดงปุ่มที่ด้านล่างของ PreferenceScreen จากนั้นใช้วิธีของ @Max มันเหมาะกับความต้องการของฉัน
นอกจากนี้ยังเป็นไปได้ที่จะเพิ่มปุ่มการดำเนินการลงในแถบการดำเนินการสำหรับแนวทางมาตรฐานของ Android
public class PrefActivity extends PreferenceActivity{
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu items for use in the action bar
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.preference_header_menu, menu);
      return super.onCreateOptionsMenu(menu);
  }
}
    <?xml version="1.0" encoding="utf-8"?>
       <menu xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:id="@+id/action_add"
           android:icon="@drawable/ic_menu_add_dark"
           android:title="@string/menu_action_add_title"
           android:showAsAction="always"  />
   </menu>
              มุมมองที่กำหนดเองในกิจกรรมการตั้งค่า สิ่งนี้จะช่วยเพิ่มมุมมองที่กำหนดเองใน PreferenceActivity ใน Android
สร้าง main.xml มุมมองที่จำเป็นเท่านั้นคือ ListView ที่มี android:id="@android:id/list"ID:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:weightSum="1">
        <ListView 
            android:id="@android:id/list" 
            android:layout_weight="1"
            android:layout_width="fill_parent"
                android:layout_height="0dp">
        </ListView>
        <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
สร้าง CustomPreferenceActivity.java
public class CustomPreferenceActivity extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                addPreferencesFromResource(R.xml.settings);
                //setup any other views that you have
                TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("View Added");
        }
}
              ต่อไปนี้เป็นวิธีง่ายๆในการเพิ่มปุ่มที่คลิกได้ในหน้าจอการตั้งค่าของคุณ สิ่งนี้ทำได้ง่ายเนื่องจากการตั้งค่าได้จองพื้นที่ใน android: widgetLayout ไว้แล้วและปุ่มสามารถส่งต่อการคลิกด้วย android: onClick
ขั้นแรกให้สร้าง button.xml พร้อมเนื้อหา
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
    android:text="BUTTON"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:onClick="onButtonClick"/>
</LinearLayout>
ตอนนี้ในการตั้งค่า. xml ของคุณเพิ่มการตั้งค่า
<Preference
    android:key="button"
    android:title="Title"
    android:summary="Summary"
    android:widgetLayout="@layout/button" />
PreferenceActivity ของคุณต้องมีสมาชิก onButtonClick เท่านั้น
public class MainActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.main_preferences);
}
public void onButtonClick(View v) {
    Log.d("Button", "Yeah, button was clicked");
}
}
              การตั้งค่า. xml:
    <Preference
        android:key="clearAllData"
        android:title="@string/settings_clear_all_data">
    </Preference>
SettingsFragment.java:
public class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
        Preference clearAllData = (Preference) findPreference("clearAllData");
        // setup buttons
        final Context context = getActivity();
        clearAllData.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                ...
            }
    }
}
              ฉันพบว่าคำตอบทั้งหมดข้างต้นไม่สามารถใช้งานได้เนื่องจากเลย์เอาต์ใด ๆ ที่ฉันสร้างขึ้นเพื่อ 'ห่อ' PreferenceScreenคอนเทนเนอร์ภายในเลย์เอาต์ที่กำหนดเอง (จากนั้นเพิ่มปุ่มด้านล่างListView) ไม่ได้ผลจริง
พวกเขาวางเฉพาะโครงร่างแบบกำหนดเองที่ด้านบนของรายการค่ากำหนด (ลอยตัว) และการคลิก (เช่น) ปุ่มกำหนดเองใหม่จะเรียกใช้เฉพาะค่ากำหนดที่อยู่ใต้ปุ่มเท่านั้น
แต่ผมพบว่าการแก้ปัญหานี้PreferenceFragmentซึ่งทำงานการรักษาสำหรับการเพิ่มปุ่มด้านล่างภาชนะรายการค่าเมื่อใช้