[英]Restrict the Width of an Android Horizontal ScrollView with nested TextView
[英]Android Horizontal Scrollview with horizontal linear layout. How can fill the whole width?
我正在做一些练习来学习android编程。
我希望这种水平布局中的每个按钮都是完整的。
因此用户可以滚动它。
这是我的xml:
<?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">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="140dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0"
android:text="Button2" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button3" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button4" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
</android.support.constraint.ConstraintLayout>
因此,按钮1和所有其他按钮必须在图库中滚动行
您使用的是HorizontalScrollView,因此match_parent的宽度无效。
您必须以编程方式将按钮的宽度设置为屏幕宽度的宽度。
尝试这个
activity_main.xml中
<?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">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="140dp">
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button2" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button3" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button4" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
linearLayout=findViewById(R.id.ll);
int width=getScreenWidth(Main2Activity.this);
int childCount=linearLayout.getChildCount();
for (int i=0;i<childCount;i++){
Button button= (Button) linearLayout.getChildAt(i);
button.setWidth(width);
}
}
public static int getScreenWidth(Context context) {
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.