Android GrDaySoT单子在使用栏数时不考虑权重
我正在尝试创建一个包含2列和无限行(通过编程添加)的网格,使用2+childs,它工作得很好,childs通常占一半的宽度,但当我添加单个子项时,它不占一半的宽度,而是填充父项 重要提示:内容是动态的,可以有单个元素或N个元素,因此它需要与单个元素和多个元素一起使用 这就是我所拥有的:Android GrDaySoT单子在使用栏数时不考虑权重,android,android-layout,android-gridlayout,Android,Android Layout,Android Gridlayout,我正在尝试创建一个包含2列和无限行(通过编程添加)的网格,使用2+childs,它工作得很好,childs通常占一半的宽度,但当我添加单个子项时,它不占一半的宽度,而是填充父项 重要提示:内容是动态的,可以有单个元素或N个元素,因此它需要与单个元素和多个元素一起使用 这就是我所拥有的: <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.GridLayout xmlns:android="http
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:orientation="horizontal"
app:columnCount="2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
android:text="This is a text that need to have width as 50% screen size "/>
</android.support.v7.widget.GridLayout >
当我添加一个与上面相同属性的新TextView时:
这就是我想要添加单个文本视图的结果:
您可以使用权重,并在线性布局中执行此操作
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_weight="2">
<TextView
android:id="@+id/leftView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="This is a text that needs to have width as 50% of screen size"
android:layout_weight="1"/>
<TextView
android:id="@+id/rightView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
可以使用权重并在线性布局中执行此操作
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_weight="2">
<TextView
android:id="@+id/leftView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="This is a text that needs to have width as 50% of screen size"
android:layout_weight="1"/>
<TextView
android:id="@+id/rightView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
我在问题中添加了一个重要的注释,如果列表中包含我不理解的奇数或偶数元素,我需要手动管理这个变通方法。如果有两列,你需要动态添加什么?@YuriHeupa我编辑了我的答案,看看这是否就是你所说的动态。我在问题中添加了一个重要的注释,如果列表中包含奇数或偶数元素,我需要手动管理这个解决方法。我不明白。如果说有两列,你需要动态添加什么?@YuriHeupa我编辑了我的答案,看看这是否就是你所说的动态。
leftView = (LinearLayout)findViewById(R.id.leftView);
TextView textView = new TextView(getApplicationContext());
textView.setText("testDynamic textView");
leftView.addView(textView);