LayoutParamsのsetMarginで設定する。
LayoutParams params = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1); params.setMargins(5, 5, 5, 5);
情報元
How to set margin of ImageView using code, not xml
LayoutParams params = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1); params.setMargins(5, 5, 5, 5);
package jp.inara.sample.preferencebug; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { Intent intent = new Intent(this, MyPreferenceActivity.class); intent.setData(Uri.parse("preferences://preference1")); startActivity(intent); return true; } }
package jp.inara.sample.preferencebug; import android.os.Bundle; import android.preference.PreferenceActivity; public class MyPreferenceActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getIntent().getData().toString().equals("preferences://preference2")) { addPreferencesFromResource(R.xml.preference2); } else { addPreferencesFromResource(R.xml.preference); } } }
ViewPager pager = (ViewPager) getActivity().findViewById(R.id.pager); FragmentStatePagerAdapter sAdapter = (FragmentStatePagerAdapter) pager.getAdapter(); HistoryListFragment f2 =(HistoryListFragment) sAdapter.instantiateItem(pager, pager.getCurrentItem());ViewPager#getCurrentItemで現在表示しているページのpositionを取得しただけですが。
java.lang.IllegalArgumentException: Binary XML file line #67: Duplicate id 0x7f050008, tag null, or parent id 0x0 with another fragment for HistoryListFragment