我有一个带有按钮的ScrollView。这些按钮是ScrollView内部的LinearLayout的一部分。
我希望ScrollView一次只显示5个按钮。每个按钮的高度应为屏幕宽度的1/5。
是否可以在不创建自定义按钮并覆盖OnMeasure方法的情况下进行操作?
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="5">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FF0000"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFFF00"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFFFFF"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#AcCDFE"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#000000"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00FF00"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000FF"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FF00FF"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00FFFF"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ABCD94"/>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
如果找不到使用XML
来实现它的方法。这是使用代码的解决方案
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
final ConstraintLayout rootLayout = findViewById(R.id.rootLayout);
rootLayout.post(new Runnable() {
@Override
public void run() {
// wait until we able to get rootLayout height then calculate and update button height
int rootLayoutHeight = rootLayout.getHeight();
LinearLayout linearLayout = findViewById(R.id.linearlayout);
for (int i = 0; i < linearLayout.getChildCount(); i++) {
Button button = (Button) linearLayout.getChildAt(i);
LinearLayout.LayoutParams layoutParams =
(LinearLayout.LayoutParams) button.getLayoutParams();
layoutParams.height = rootLayoutHeight / 5;
button.setLayoutParams(layoutParams);
}
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFF00"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#AcCDFE"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF00FF"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFF"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ABCD94"
/>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
它有1个缺点:使用rootLayoutHeight/5
时,它将返回 float ,但是您不能将 float 设置为按钮高度(因为{{ 1}}需要 int )=>您将失去一些小价值
答案 1 :(得分:0)
从XML文件中的android:weightSum="5"
和LinearLayout
中删除android:layout_weight="1"
和Button
,并设置所有按钮的高度= 屏幕高度/ 5 从Java文件中获取。