水平LinearLayout以相反的顺序显示组件 [英] Horizontal LinearLayout displaying components in reverse order

查看:63
本文介绍了水平LinearLayout以相反的顺序显示组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LinearLayout(list_alphabet.xml),其中包含一个ListViewLinearLayout. 方向没有明确设置,因此是水平的. 我希望ListView出现在其左侧,然后是其右侧的第二个"LinearLayout"(垂直), 但实际上它们的顺序相反. 布局被添加到MainActivity的Fragment中.
请让我知道是否还有另一段代码可以显示出帮助.

I have a LinearLayout (list_alphabet.xml), which contains a ListView and a LinearLayout. The orientation is not explicitly set and therefore is horizontal. I would expect the ListView to appear on the left and then the 2nd 'LinearLayout' (which is vertical) to the right of it, but in fact they are in reverse order. The layout is added to a Fragment within MainActivity.
Please let me know if there is another piece of code to show that would be helpful.

如何使它们以在XML布局文件中出现的顺序出现?

How can I get them to appear in the order that they appear in the xml layout file?

MainActivity

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

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代码:

This is the Java code for the side index:

    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);
    }
}

推荐答案

@ Ashley 说的差不多,但是似乎对我有用::

Pretty much what @Ashley said, but it seems to work for me::

<?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 .

Note: Once you've added content to your inner linear layout, change the width to wrap_content.

这篇关于水平LinearLayout以相反的顺序显示组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆