我有LinearLayout
(list_alphabet.xml),其中包含ListView
和LinearLayout
。
方向未明确设置,因此是水平的。
我希望ListView
出现在左边,然后是第二个' LinearLayout' (它是垂直的)在它的右边,
但实际上它们的顺序是相反的。
布局将添加到MainActivity中的片段中
如果有另外一段代码可以帮助我,请告诉我。
如何让它们按照它们在xml布局文件中出现的顺序显示?
MainActivity
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_alphabet);
fragmentTransaction.add(R.id.list_alphabet_layout, new MyListFragment(), getResources().getString(R.string.list_fragment_tag));
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
...
}
list_alphabet.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_alphabet_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:fastScrollEnabled="true" />
<LinearLayout
android:id="@+id/sideIndex"
android:layout_width="60dip"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
这是副索引的Java代码:
public void updateList() {
// setup the side index (the column of letters)
LinearLayout sideIndexLayout = (LinearLayout) getActivity().findViewById(R.id.sideIndex);
sideIndexLayout.removeAllViews();
indexListSize = alphabetList.size();
if (indexListSize < 1) {
return;
}
int indexMaxSize = (int) Math.floor(sideIndexLayout.getHeight() / 20);
int tmpIndexListSize = indexListSize;
while (tmpIndexListSize > indexMaxSize) {
tmpIndexListSize = tmpIndexListSize / 2;
}
double delta;
if (tmpIndexListSize > 0) {
delta = indexListSize / tmpIndexListSize;
}
else {
delta = 1;
}
TextView tempTextView;
for (double i = 1; i <= indexListSize; i = i + delta) {
Object[] tmpIndexItem = alphabetList.get((int) i - 1);
String tmpLetter = tmpIndexItem[0].toString();
tempTextView = new TextView(getActivity());
tempTextView.setTextColor(getResources().getColor(android.R.color.white));
tempTextView.setText(tmpLetter);
tempTextView.setGravity(Gravity.CENTER);
tempTextView.setTextSize(15);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
tempTextView.setLayoutParams(params);
sideIndexLayout.addView(tempTextView);
}
sideIndexHeight = sideIndexLayout.getHeight();
sideIndexLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
sideIndexX = event.getX(); // coordinates of the touch
sideIndexY = event.getY();
// and can display a proper item in name list
displayListItem();
return false;
}
});
}
public void displayListItem() {
LinearLayout sideIndexLayout = (LinearLayout) getActivity().findViewById(R.id.sideIndex);
sideIndexLayout.setBackgroundColor(getResources().getColor(R.color.emnrd_green));
sideIndexHeight = sideIndexLayout.getHeight();
// compute number of pixels for every side index item
double pixelPerIndexItem = (double) sideIndexHeight / indexListSize;
// compute the item index for given event position belongs to
int itemPosition = (int) (sideIndexY / pixelPerIndexItem);
// get the item (we can do it since we know item index)
if (itemPosition < alphabetList.size()) {
Object[] indexItem = alphabetList.get(itemPosition);
int subitemPosition = sectionMap.get(indexItem[0]);
//ListView listView = (ListView) findViewById(android.R.id.list);
getListView().setSelection(subitemPosition);
}
}
答案 0 :(得分:1)
抱歉无法发表评论,但这很有意思。解决方法可能是使用相对布局。只需将它们设置为正常,甚至可以进行设置,以便将listview设置为左侧。如果这不起作用那么有趣。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_alphabet_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/sideIndex"
android:layout_width="60dip"
android:layout_alignParentRight="true"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_marginRight="60dp"
android:layout_alignParentLeft="true"
android:layout_height="match_parent"
android:fastScrollEnabled="true" />
</RelativeLayout>
答案 1 :(得分:1)
几乎是@ Ashley所说的,但它似乎对我有用::
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_alphabet_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/sideIndex"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@color/material_blue_grey_800"
android:gravity="center_horizontal"
android:orientation="vertical"></LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/sideIndex"
android:fastScrollEnabled="true" />
</RelativeLayout>
注意:在内部线性布局中添加内容后,请将宽度更改为 wrap_content 。