Android:调整图像按钮和文本视图的大小?
我正在尝试将3个文本视图和图像按钮添加到我的android布局中。我一直在尝试调整它们的大小,以便所有6个组件都适合屏幕 我相信这是一个非常简单的问题,但我无法解决它,它非常模糊 我怎样才能做到 布局代码:Android:调整图像按钮和文本视图的大小?,android,android-layout,textview,imagebutton,Android,Android Layout,Textview,Imagebutton,我正在尝试将3个文本视图和图像按钮添加到我的android布局中。我一直在尝试调整它们的大小,以便所有6个组件都适合屏幕 我相信这是一个非常简单的问题,但我无法解决它,它非常模糊 我怎样才能做到 布局代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvFirstTabMaths"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maths"
android:textSize="15dp" />
<ImageButton
android:id="@+id/ibFirstTabMathsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mathsicon" />
<TextView
android:id="@+id/tvFirstTabMemory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Memory"
android:textSize="15dp" />
<ImageButton
android:id="@+id/ibFirstTabMemoryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/memoryicon" />
<TextView
android:id="@+id/tvFirstTabStroop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stroop"
android:textSize="15dp" />
<ImageButton
android:id="@+id/ibFirstTabStroopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stroopicon" />
</LinearLayout>
如果要让它们填满屏幕,需要使用
布局\权重属性
将所有android:layout\u height=“wrap\u content”
更改为android:layout\u height=“0dp”
并添加android:layout\u weight=“1”
权重使视图共享可用空间
重量可以是浮子,也可以是你想要的任何东西。这就像比率,如果你有一个在1,另一个在2,第二个将是第一个的两倍大
您也可以在线性布局上使用weight\u sum=“10”
,如果您给孩子视图的权重小于10,则会留下空白。例如,你可以用按钮获得屏幕的6/10,而4/10是空的
也可以将没有权重的视图放置在具有权重的视图的容器中。第一个视图将占用其空间,剩余空间将与其他视图共享。如果您使用的是线性布局,则应将android:layout\u权重设置为儿童。来自文档:“此属性指定”重要性“视图在屏幕上应占据多少空间方面的价值。较大的权重值允许其展开以填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组中的任何剩余空间都将按其声明权重的比例分配给子视图“尝试执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<TextView android:id="@+id/tvFirstTabMaths"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Maths" android:textSize="15dp" android:layout_weight="1" />
<ImageButton android:id="@+id/ibFirstTabMathsButton"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:src="@drawable/mathsicon" android:layout_weight=".5" />
<TextView android:id="@+id/tvFirstTabMemory"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text=" Memory" android:textSize="15dp" android:layout_weight="1" />
<ImageButton android:id="@+id/ibFirstTabMemoryButton"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:src="@drawable/memoryicon" android:layout_weight=".5" />
<TextView android:id="@+id/tvFirstTabStroop"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Stroop" android:textSize="15dp" android:layout_weight="1" />
<ImageButton android:id="@+id/ibFirstTabStroopButton"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:src="@drawable/stroopicon" android:layout_weight=".5" />
</LinearLayout>
</ScrollView>
如果您希望所有6个视图水平匹配,则应尝试通过编程获得屏幕大小,然后设置视图宽度/参数
视图。设置宽度(宽度/6);
谢谢这是一个很酷的解决方案,但是,有没有办法使每个Imagebutton的大小相同。此时,其中一个按钮比其他按钮大得多,为了保持一致性,我希望它们的大小都相同?感谢您的编辑,但我无法使用此解决方案,因为我遇到了一个错误:找不到Attribute的资源标识符“安卓”软件包中的“权重”很抱歉输入错误,更正了它。希望它现在能工作。我解决了问题,它应该是布局权重,虽然这已经解决了图像大小不同的问题,但它们在屏幕上仍然很大,我怎样才能将图像更改为更小?(大约是当前大小的一半)谢谢,请看我的最新评论!:)你帮了我很大的忙!它们是水平放置的,我正在使用另一个答案中建议的代码。我只希望每个图像按钮都更小?这样它就不会占用屏幕那么多空间?谢谢