我必须将单个线性布局拆分为两列(像报纸列)。线性布局包含文本视图和图像视图< / strong>
我已经占用了屏幕宽度并将其分为一半,并使TextView
和ImageView
进入第一列,即下图中的A B C
块。现在,剩余的TextView
和“ImageView
必须像D E F
一样进入下一列,就像它继续下去一样。所以如果有人给我任何代码或想法来实现这一点会有所帮助..我试过GridView
这不适合我的问题。 由于TextView
和ImageView
尺寸不明确。
我不知道如何拆分Liner布局。 我尝试计算rootlayout高度 像这样
linearLayout.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int linsize=linearLayout.getHeight();
int relsize=root.getHeight();
int textsize=txt1.getHeight();
mainheight=relsize;
subheight=linsize;
Toast.makeText(getApplicationContext(), "Linerlayout "+linsize, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Relative layout"+relsize, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "text height "+textsize, Toast.LENGTH_LONG).show();
if(mainheight==subheight)
{
Toast.makeText(getApplicationContext(), "make a new linear layout", Toast.LENGTH_LONG).show();
createsubview();
}
}
});
屏幕截图
答案 0 :(得分:6)
您可以使用嵌套的LinearLayouts
:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
content here/>
<TextView
content here/>
</LinearLayout>
</LinearLayout>
然后您需要做的就是将A,B和C放在第一个垂直布局中,将D,E和F放在第二个布局中。
答案 1 :(得分:1)
使用GridView无法做到这一点。您必须创建自定义视图才能执行此操作。
如果你知道你的网格物品有多大,你可以削减一些角落。 GridView很复杂,主要是因为它处理任何大小的项目并动态加载它们。对您来说更简单的方法可能是:
1.在里面创建一个水平LinearLayout的HorizontalScrollView。
2.确定您的项目的行数适合屏幕。称这行为。
3.你仍然需要布置项目:
1.Create a vertical LinearLayout, adding rows or less items to it.
2.Add your new vertical LinearLayout to the horizontal one.
“水平GridView”会给你带来一些缺点:
1.All the views are loaded up immediately, which is bad for huge lists of items.
2.You need to know how big your items are, and they need to be the same size.
上升空间:
1.It's very easy to implement.
for more inf plz see this link
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollView = new ScrollView(this);//ScrollView
LinearLayout ll = new LinearLayout(this); //root LinearLayout
ll.setOrientation(LinearLayout.HORIZONTAL);//with horizontal orientation
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1f);
LinearLayout l2 = new LinearLayout(this); //sub linearlayout
l2.setOrientation(LinearLayout.VERTICAL);//with vertical orientation
l2.setLayoutParams(layoutParams);
LinearLayout l3 = new LinearLayout(this); //sub linearlayout
l3.setOrientation(LinearLayout.VERTICAL);//with vertical orientation
l3.setLayoutParams(layoutParams);
int totalvalues=41; //i take count as 41
for(int i=0;i<totalvalues;i++){ // add the buttons in the layout based on condition
Button okButton=new Button(this);
okButton.setText("Button"+i);
if(i<=totalvalues/2){
l2.addView(okButton);
}
else{
l3.addView(okButton);
}
}
ll.addView(l2); //add sub linearlayout to root linearlayout
ll.addView(l3); //add sub linearlayout to root linearlayout
scrollView.addView(ll); //add the root linearlayout to scrollview
setContentView(scrollView);
}
答案 2 :(得分:0)
你试过了吗?
DisplayMetrics metrics = getResources().getDisplayMetrics();
float dpW = 0f;
int pixelsW = (int) (metrics.density * dpW + 0.5f);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(pixelsW, LayoutParams.WRAP_CONTENT, 1f);
TextView txt = new TextView(MainActivity.this);
ImageView img = new ImageView(MainActivity.this);
txt.setLayoutParams(lp);
img.setLayoutParams(lp);
使用TableLayout的LayoutParams,您可以设置视图的权重,如您所知,必须为1.我们还使用DisplayMetrics将float转换为&#34; dp&#34; xml中使用的格式。
编辑:
您也可以将此LayoutParams设置为LinearLayout。