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); } } }