如何在android上集中按钮?
我很难创建一个布局,比如图像左侧的对话框。但我想用水平方向来创建它。我希望我的布局有这个,但只有两个按钮水平 我的问题是布局xml文件,我不知道如何启动此布局=( 真正的问题是如何编辑xml文件以使按钮集中,我尝试了很多想法、填充、方向、对齐等…但我无法对齐。我当前的布局是。如何集中按钮 有人能帮我吗 这是我的xml文件代码如何在android上集中按钮?,android,android-layout,android-view,android-dialog,Android,Android Layout,Android View,Android Dialog,我很难创建一个布局,比如图像左侧的对话框。但我想用水平方向来创建它。我希望我的布局有这个,但只有两个按钮水平 我的问题是布局xml文件,我不知道如何启动此布局=( 真正的问题是如何编辑xml文件以使按钮集中,我尝试了很多想法、填充、方向、对齐等…但我无法对齐。我当前的布局是。如何集中按钮 有人能帮我吗 这是我的xml文件代码 <?xml version="1.0" encoding="utf-8"?> 有人能帮我吗?首先,您需要使用
<?xml version="1.0" encoding="utf-8"?>
有人能帮我吗?首先,您需要使用您提到的按钮创建所需的布局(我想您可以自己完成)。然后,您可以使用该布局来为
警报对话框充气,正如我在下面所做的:
View alertView = getLayoutInflater().inflate(
R.layout.your_custom_layout,
(ViewGroup) findViewById(R.id.alertViewLayout));
new AlertDialog.Builder(this)
.setTitle(title)
.setView(alertView)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// handle click here
// dismiss AlertDialog
dialog.dismiss();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// handle click here
// dismiss AlertDialog
dialog.dismiss();
}
}).show();
您可以通过引用父视图(alertView
)来访问AlertDialog
布局中的视图
编辑
这是一个示例布局文件,其中2个imagebutton
并排放置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/alertViewLayout"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:layout_weight=".5"
android:background="@+drawable/imageButton1"
android:text="Button 1" />
<ImageButton
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_weight=".5"
android:background="@+drawable/imageButton2"
android:text="Button 2" />
</LinearLayout>
编辑2
由于您希望按钮具有自定义背景,我建议您使用ImageButton
并将background
属性设置为您选择的背景
如果需要有关如何创建布局文件的更多详细信息,可以选中
请让我知道这是否有帮助。您的问题能更清楚一些吗?我不知道我是否答对了,但我会尽力解释
首先,如果您想为对话框创建一个自定义布局,您应该创建一个.xml文件,然后用这个特定的布局将其膨胀
但我认为这不是你的情况,你想做的只是创建一个自定义对话框,对吗?屏幕方向并不重要(我不知道你是不是有意的)
我不认为我应该向您解释整个过程或只是粘贴代码,您最好使用android官方文档,因此请查看以下链接:
希望它能帮助你。最简单的答案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation = "horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:id="@+id/textView6"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:id="@+id/textView7"/>
</LinearLayout>
您可以这样做:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="@+id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/button2" />
</LinearLayout>
</RelativeLayout>
我真的很感谢你的帮助…抱歉我的解释,但我的问题是布局。我是安卓布局的新手,我不知道如何创建布局。你能帮助我吗?按钮继续不集中……参见这里:@ CAAARLOS我再次更新我的答案。考虑以父布局为中心。同样的问题。我以编程方式集中父布局?@Caaarlos我再次用android:gravity=“center”更新了我的答案
@Grabriel R.,我编辑了我的答案,我真的很抱歉我的解释不好。嗨,你的回复给了我相同的@IRuth视觉效果,但这几乎是我想要的。看看我想要的视觉效果,它看起来是一个父布局中的两个布局,这些布局的每个按钮都是集中的……你能帮我制作一个像上面sc一样的布局吗重新拍摄?我想要的视频:当前视频:谢谢你的帮助!!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation = "horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:id="@+id/textView6"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Large Text"
android:id="@+id/textView7"/>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="@+id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/button2" />
</LinearLayout>
</RelativeLayout>