java在安卓中动态添加按钮和布局
以下是使用布局资源文件的代码:
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical"
安卓:clipChildren="false"
安卓:clipToPadding="false"
tools:context=".MainActivity">
<LinearLayout
安卓:id="@+id/layout1"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:clipChildren="false"
安卓:clipToPadding="false"
>
<Button
安卓:id="@+id/im11"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:layout_weight="1"
安卓:src="@drawable/munshee_logo"
安卓:padding="1dp"
安卓:scaleType="centerCrop"
安卓:cropToPadding="true"
安卓:clipChildren="false"
安卓:clipToPadding="false"
安卓:background="@drawable/munshee_logo"/>
<Button
安卓:id="@+id/im12"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:layout_weight="1"
安卓:padding="1dp"
安卓:scaleType="centerCrop"
安卓:cropToPadding="true"
安卓:clipChildren="false"
安卓:clipToPadding="false"
安卓:background="@drawable/munshee_logo"
安卓:src="@drawable/munshee_logo" />
<Button
安卓:id="@+id/im13"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:layout_weight="1"
安卓:padding="1dp"
安卓:scaleType="centerCrop"
安卓:cropToPadding="true"
安卓:clipChildren="false"
安卓:clipToPadding="false"
安卓:background="@drawable/munshee_logo"
安卓:src="@drawable/munshee_logo" />
<Button
安卓:id="@+id/im14"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:layout_weight="1"
安卓:padding="1dp"
安卓:scaleType="centerCrop"
安卓:cropToPadding="true"
安卓:clipChildren="false"
安卓:clipToPadding="false"
安卓:background="@drawable/munshee_logo"
安卓:src="@drawable/munshee_logo" />
<Button
安卓:id="@+id/im15"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:layout_weight="1"
安卓:padding="1dp"
安卓:scaleType="centerCrop"
安卓:cropToPadding="true"
安卓:clipChildren="false"
安卓:clipToPadding="false"
安卓:background="@drawable/munshee_logo"
安卓:src="@drawable/munshee_logo" />
</LinearLayout>
如何将其转换为java代码?结果应该是一排按钮水平分布在屏幕上。此外,每个按钮应根据屏幕的尺寸进行缩放。请帮我实现这个
# 1 楼答案
在java中的布局上添加视图和组件与xml非常相似。 简单的示例方法如下:-
# 2 楼答案
您可以尝试动态添加按钮:
在Java类中:
编辑: