我正在嘗試將HorizontalScrollView與子LinearLayout視圖一起使用。 我想要一個項目用邊距填滿屏幕,並且下一個項目能夠在滾動後看到。爲了讓你明白,我會告訴你我用代碼試過的一切。layout_margin在添加視圖組中的視圖時被忽略
首先,我固定了物品的寬度,高度和邊距。
<com.example.kanbanboard.TaskListView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="360dp"
android:layout_height="360dp"
android:layout_margin="10dp"
android:background="@drawable/container">
<TextView
android:id="@+id/category"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
android:layout_margin="10dp"/>
<ListView
android:id="@+id/list"
android:layout_margin="10dp"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</com.example.kanbanboard.TaskListView>
這位家長的佈局
<com.example.kanbanboard.MyHorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="1dp" // It seems that whether 1dp or fill_parent does not matter
android:layout_height="1dp"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<LinearLayout
android:id="@+id/list_container"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000000">
</LinearLayout>
</com.example.kanbanboard.MyHorizontalScrollView>
,這裏是其中添加的線性佈局視圖代碼
public void init(){
LayoutInflater inflater = LayoutInflater.from(getContext());
ViewGroup parent = (ViewGroup) getChildAt(0);
//CategoryView is almost same with TaskListView
CategoryView categoryList = (CategoryView) inflater.inflate(R.layout.category_layout, parent, false);
TaskListView taskList = (TaskListView) inflater.inflate(R.layout.task_list_layout,
parent, false);
categoryList.init(taskList);
taskList.init();
View[] children = new View[]{categoryList, taskList};
ViewTreeObserver.OnGlobalLayoutListener listener = new MainLayoutListener(parent, children);
getViewTreeObserver().addOnGlobalLayoutListener(listener);
}
class MainLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener{
ViewGroup parent;
View[] children;
public MainLayoutListener(ViewGroup parent, View[] children){
this.parent = parent;
this.children = children;
}
@Override
public void onGlobalLayout(){
ScrollView me = ScrollView.this;
me.getViewTreeObserver().removeGlobalOnLayoutListener(this);
parent.addView(children[0]);
parent.addView(children[1]);
}
}
結果:http://i.imgur.com/zIe1gTN.png?1
的利潤率爲不忽略,但該項目不適合屏幕。 於是,我就在layout_width和layout_height使用FILL_PARENT,但結果卻是
http://imgur.com/y2yNWmz&zIe1gTN#0
我也試着設置寬度,高度和用的LayoutParams保證金。
ScrollView me = ScrollView.this;
me.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int w = me.getMeasuredWidth() - 50 // for margin;
int h = me.getMeasuredHeight() - 50;
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(w, h);
params.setMargins(10, 10, 10, 10);
parent.addView(children[0], params);
parent.addView(children[1], params);
使用上面的代碼,我可以將項目放到屏幕上,但邊距被忽略。
我希望你能理解我的問題。我該如何解決這種情況?