Android 滚动视图中的权重
在我的应用程序中,我试图创建一个滚动视图,其中有3个大按钮,填满整个屏幕。所以每个按钮的大小是屏幕的1/3。我该怎么做? 当我使用砝码时,它不起作用Android 滚动视图中的权重,android,scrollview,Android,Scrollview,在我的应用程序中,我试图创建一个滚动视图,其中有3个大按钮,填满整个屏幕。所以每个按钮的大小是屏幕的1/3。我该怎么做? 当我使用砝码时,它不起作用 <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/imageView2"
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/header_layout" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/main_btn_call" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/main_btn_unlock" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/main_btn_push" />
</TableRow>
</TableLayout>
</ScrollView>
您使用的是
表格布局
,这是一个线性布局
,方向设置为垂直
——到目前为止,这一切都很好,您可以在布局高度
属性上使用权重
我看到的问题是,您在
滚动视图
和表格布局
中都使用了wrap\u内容
。权重值允许视图展开以填充可用空间,但父视图正在将内容包装为0+0+0。如果将两个父元素的高度更改为match\u parent
,则权重值将有空间展开您使用的是TableLayout
这是一个LinearLayout
,方向设置为vertical
-到目前为止,使用layout\u height
属性上的权重都很好
我看到的问题是,您在
滚动视图
和表格布局
中都使用了wrap\u内容
。权重值允许视图展开以填充可用空间,但父视图正在将内容包装为0+0+0。如果您将两个父元素的“高度”更改为“匹配父元素”,则您的权重值将有展开的空间我选择使用代码执行此操作:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView incallImage = (ImageView) findViewById(R.id.incall_image);
final ImageView unlockImage = (ImageView) findViewById(R.id.unlock_image);
final ImageView pushImage = (ImageView) findViewById(R.id.push_image);
final ScrollView scrollViewMain = (ScrollView) findViewById(R.id.scroll_view_main);
scrollViewMain.post(new Runnable() {
@Override
public void run() {
scrollMainHeight = scrollViewMain.getHeight();
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, scrollMainHeight / 3);
incallImage.setLayoutParams(layoutParams);
unlockImage.setLayoutParams(layoutParams);
pushImage.setLayoutParams(layoutParams);
}
});
}
如果有人能在xml中找到这样做的方法,那就太好了!,
谢谢 我想用代码来实现这一点:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView incallImage = (ImageView) findViewById(R.id.incall_image);
final ImageView unlockImage = (ImageView) findViewById(R.id.unlock_image);
final ImageView pushImage = (ImageView) findViewById(R.id.push_image);
final ScrollView scrollViewMain = (ScrollView) findViewById(R.id.scroll_view_main);
scrollViewMain.post(new Runnable() {
@Override
public void run() {
scrollMainHeight = scrollViewMain.getHeight();
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, scrollMainHeight / 3);
incallImage.setLayoutParams(layoutParams);
unlockImage.setLayoutParams(layoutParams);
pushImage.setLayoutParams(layoutParams);
}
});
}
如果有人能在xml中找到这样做的方法,那就太好了!,
谢谢 唯一的更改是在XML中添加以下属性:
android:fillViewport="true"
并且您的滚动视图宽度和高度应该是匹配\u parent
android:layout_width="match_parent"
android:layout_height="match_parent"
唯一的更改是在XML中添加以下属性:
android:fillViewport="true"
并且您的滚动视图宽度和高度应该是匹配\u parent
android:layout_width="match_parent"
android:layout_height="match_parent"
这对我不起作用。它以什么方式不起作用?它没有改变任何东西。你没有在问题中描述最初的问题是什么(显示的是什么,这种“不起作用”的方式是什么)。问题是图像没有根据权重自我调整大小,它们的行为就像没有权重一样。只需将一个堆叠在另一个上,而不填充屏幕。这对我不起作用。它以什么方式不起作用?它没有改变任何东西。你没有在问题中描述最初的问题是什么(显示什么以及这种“不起作用”的方式是什么)。问题是图像没有根据权重自行调整大小,他们表现得好像里面没有重物。只需将一个堆叠在另一个上,而不填充屏幕。作为按钮使用的图像的确切大小是多少?为什么大小是最大的?它们大约是150 DP。请定义“它们不工作”。不要忘记,你需要为你的问题提供一个主题。你用作按钮的图像的确切大小是多少?为什么是大小?它们大约是150 DP。请定义“它们不工作”。别忘了,你需要为你的问题提供一个主题。