我正在尝试创建一个包含ScrollView的活动。在ScrollView中将出现“be”(有点像按钮),它比父视图的宽度要小一些,并且垂直地分开。因为盒子上有几个不同大小的文本,所以我想我需要用RelativeLayouts来制作。(我会发一张照片,但不能以我的声誉为出发点,所以希望这是有意义的)。
我尝试为该活动创建一个XML,而不是编程地添加相对视图。我已经开始工作了,有点,但是我的盒子间距有问题。
我的测试XML看起来像这样..。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- scrolling section -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="@+id/llScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>我的活动看起来是这样..。
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout llAccts=(LinearLayout)findViewById(R.id.llScroll);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
llAccts.addView(rlView,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
}我得到了可滚动的部分,没问题。问题是,我的蓝色“盒子”似乎是互相接触,我不知道如何使它们看起来像按钮与填充,左,右,和他们之间。我试过了
rlView.setPadding(20,20,20,20);就在setBackground颜色之后。所有看上去都是贴上文字..。不是RelativeViews。
有什么建议没办法让它起作用吗?
谢谢
===========
新信息:以下是一些建议和尝试错误后的其他信息。我还可以张贴一张图片,以便于参考。
===========
我想要达到的目标,从程序上来说,是这样的。

我使用了直截了当的XML实现了这个例子,它看起来像.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="@+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="@+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
// individual groups start here - this section will be controlled by a database
// #1 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="@color/blue"
android:padding="5dp"
android:clickable="true"
android:id="@+id/grp1"
>
<TextView
android:id="@+id/text1"
android:text="Item #1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/leftext1"
android:text="123.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp" />
</RelativeLayout>
// #2 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@color/blue"
android:padding="5dp"
android:clickable="true"
android:id="@+id/grp2"
>
<TextView
android:id="@+id/text2"
android:text="Item #2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/leftext2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/leftext2"
android:text="456.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp"
/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>我把它拆开了,创建了一个XML文件和活动,它将以编程方式创建中间组。
修改后的XML (activity_main2)如下.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="@+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="@+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>我开始重写我的活动代码来测试一些建议。现在看起来是这样..。
public class Main2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
LinearLayout llGroups=(LinearLayout)findViewById(R.id.llGroups);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.setMargins(10,10,10,0);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
// other textviews will go here
llGroups.addView(rlView,rlp);
}
}
}这会编译并运行,但是setMargins不起作用。我的屏幕顶部有"UPDATE ALL“按钮,就像它应该的那样,(19) "Text #”有蓝色的背景,但是它们都是全宽的,互相触摸,所以它是一个纯蓝色的背景。
我正在接近,但不确定如何为rlView相对视图(而不是文本视图)正确设置页边距。
希望这能解释我现在看到的..。
溶液
经过建议和一些尝试和错误..。这里是我必须做的,以实现理想的布局,以防其他人感兴趣。
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout mTvContainer = (LinearLayout) findViewById(R.id.llAccts);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.BLUE);
RelativeLayout rl = new RelativeLayout(this);
TextView tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setPadding(5, 5, 5, 5);
tv.setText("Test #" + i);
tvp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(5, 5, 5, 5);
tv2.setText(String.valueOf(i));
tv2.setTextSize(30);
tvp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(tv, tvp);
rl.addView(tv2,tvp2);
ll.addView(rl);
llp.setMargins(32,16,32,16);
mTvContainer.addView(ll,llp);
}
}对于FOR中的每个条目,我必须使用LINEARLAYOUT来正确设置页边距,而在这个LINEARLAYOUT中,我必须使用RELATIVELAYOUT来使用addRules来获得TextViews的正确位置。
如果有更简单的方法,请告诉我。谢谢大家的帮助!!
发布于 2015-09-16 04:04:58
您正在使用太多的嵌套布局所需,这里是剥离版本。
xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ll_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView> 活动代码:
mTvContainer = (LinearLayout) findViewById(R.id.ll_main);
int p = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setBackgroundColor(Color.BLUE);
tv.setTextColor(Color.WHITE);
tv.setPadding(p, p / 2, p, p / 2);
tv.setText("TextView " + i);
TextView tv2 = new TextView(this);
tv2.setBackgroundColor(Color.RED);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(p, p / 2, p, p / 2);
tv2.setText("TextView " + i);
ll.addView(tv);
ll.addView(tv2);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(p, p / 2, p, p / 2);
mTvContainer.addView(ll, params);
}https://stackoverflow.com/questions/32599352
复制相似问题