我希望EditText的默认余量为10dp.因此,在我的styles.xml文件中,我设置了以下内容:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:editTextStyle">@style/edit_text_default</item>
</style>
<style name="edit_text_default" parent="android:style/Widget.EditText">
<item name="android:layout_margin">10dp</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后在AndroidManifest.xml中,我将应用程序主题设置为我定义的主题:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
...
Run Code Online (Sandbox Code Playgroud)
该主题的"无标题栏"方面正在发挥作用.但是,EditText的默认边距不是,它仍然填充父级.这是我的表格视图:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TableRow>
<EditText android:hint="@string/last_name" />
</TableRow>
<TableRow>
<EditText android:hint="@string/first_name" />
</TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud) 我在android aplication中组织布局有问题.我正在动态创建按钮并将这些代码添加到我的布局中:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < NO_NUMBERS; i++){
Button btn = new Button(this);
btn = (Button) layoutInflater.inflate(R.layout.button, null);
btn.setId(2000+i);
Integer randomNumber = sort.getNumbersCopy()[i];
btn.setText(randomNumber.toString());
btn.setOnClickListener((OnClickListener) this);
buttonList.addView(btn);
list.add(btn);
}
Run Code Online (Sandbox Code Playgroud)
我将它添加到LinearLayout:
<LinearLayout
android:id="@+id/buttonlist"
android:layout_alignParentLeft="true"
android:layout_marginTop="185dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我正在导入这个.xml我在哪里定义按钮布局:
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="26dp"
android:textStyle ="bold"
android:textColor="#ffffff"
android:background="@drawable/button"
android:layout_marginLeft="8px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
Run Code Online (Sandbox Code Playgroud)
好吧,布局总是这样结束:
而不是像这样的东西(甚至按钮,方形按钮之间的间距):

总结一下:我必须:
使用XML文件很简单,因为我可以将参数指定为
<android:layout_width="fill_parent" android:layout_height="wrap_content">
Run Code Online (Sandbox Code Playgroud)
但我在通过代码指定它时感到困惑.对于每个视图,我使用指定参数
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
Run Code Online (Sandbox Code Playgroud)
我看到我可以选择将其指定为相对布局,框架布局等.截至目前,我正在为所有视图使用线性布局,例如图像,文本和gridview.是否应根据父元素的布局定义视图参数?或者将它指定为线性布局是否可以,即使视图是例如framelayout的子节点?对不起,但我找不到差异.